-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add a recipe for saving and restoring
state
externally (#10722)
Signed-off-by: Michel Engelen <[email protected]> Co-authored-by: Andrew Cherniavskii <[email protected]>
- Loading branch information
1 parent
391351c
commit 11f2867
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
docs/data/data-grid/state/SaveAndRestoreStateInitialState.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import CircularProgress from '@mui/material/CircularProgress'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
import { DataGridPremium, useGridApiRef } from '@mui/x-data-grid-premium'; | ||
|
||
export default function SaveAndRestoreStateInitialState() { | ||
const apiRef = useGridApiRef(); | ||
const { data, loading } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 100, | ||
maxColumns: 10, | ||
}); | ||
|
||
const [initialState, setInitialState] = React.useState(); | ||
|
||
React.useEffect(() => { | ||
const stateFromLocalStorage = localStorage?.getItem('dataGridState'); | ||
setInitialState(stateFromLocalStorage ? JSON.parse(stateFromLocalStorage) : {}); | ||
}, []); | ||
|
||
const saveSnapshot = React.useCallback(() => { | ||
if (apiRef?.current && localStorage) { | ||
const currentState = apiRef.current.exportState(); | ||
localStorage.setItem('dataGridState', JSON.stringify(currentState)); | ||
} | ||
}, [apiRef]); | ||
|
||
if (!initialState) { | ||
return <CircularProgress />; | ||
} | ||
|
||
return ( | ||
<Box sx={{ height: 300, width: '100%' }}> | ||
<DataGridPremium | ||
{...data} | ||
apiRef={apiRef} | ||
loading={loading} | ||
onStateChange={saveSnapshot} | ||
initialState={{ | ||
...data.initialState, | ||
...initialState, | ||
}} | ||
/> | ||
</Box> | ||
); | ||
} |
48 changes: 48 additions & 0 deletions
48
docs/data/data-grid/state/SaveAndRestoreStateInitialState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import CircularProgress from '@mui/material/CircularProgress'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
import { DataGridPremium, useGridApiRef } from '@mui/x-data-grid-premium'; | ||
import { GridInitialStatePremium } from '@mui/x-data-grid-premium/models/gridStatePremium'; | ||
|
||
export default function SaveAndRestoreStateInitialState() { | ||
const apiRef = useGridApiRef(); | ||
const { data, loading } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 100, | ||
maxColumns: 10, | ||
}); | ||
|
||
const [initialState, setInitialState] = React.useState<GridInitialStatePremium>(); | ||
|
||
React.useEffect(() => { | ||
const stateFromLocalStorage = localStorage?.getItem('dataGridState'); | ||
setInitialState(stateFromLocalStorage ? JSON.parse(stateFromLocalStorage) : {}); | ||
}, []); | ||
|
||
const saveSnapshot = React.useCallback(() => { | ||
if (apiRef?.current && localStorage) { | ||
const currentState = apiRef.current.exportState(); | ||
localStorage.setItem('dataGridState', JSON.stringify(currentState)); | ||
} | ||
}, [apiRef]); | ||
|
||
if (!initialState) { | ||
return <CircularProgress />; | ||
} | ||
|
||
return ( | ||
<Box sx={{ height: 300, width: '100%' }}> | ||
<DataGridPremium | ||
{...data} | ||
apiRef={apiRef} | ||
loading={loading} | ||
onStateChange={saveSnapshot} | ||
initialState={{ | ||
...data.initialState, | ||
...initialState, | ||
}} | ||
/> | ||
</Box> | ||
); | ||
} |
10 changes: 10 additions & 0 deletions
10
docs/data/data-grid/state/SaveAndRestoreStateInitialState.tsx.preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<DataGridPremium | ||
{...data} | ||
apiRef={apiRef} | ||
loading={loading} | ||
onStateChange={saveSnapshot} | ||
initialState={{ | ||
...data.initialState, | ||
...initialState, | ||
}} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as React from 'react'; | ||
|
||
export default function Playground() { | ||
return ( | ||
<div>A playground for a fast iteration development cycle in isolation outside of git.</div> | ||
); | ||
} |