|
1 | | -import './App.css' |
2 | | -import { decrement, increment } from './state/counter/reducer' |
3 | | -import { useAppDispatch, useAppSelector } from './state/hooks' |
| 1 | +import './App.css'; |
| 2 | +import { counterSlice } from './state/counter/slice'; |
| 3 | +import { useAppDispatch, useAppSelector } from './state/hooks'; |
| 4 | +import { setPaused } from './state/status/slice'; |
| 5 | +import { store as storePromise } from './state/store'; |
4 | 6 |
|
5 | 7 |
|
6 | 8 | function App() { |
7 | | - // The `state` arg is correctly typed as `RootState` already |
8 | | - const count = useAppSelector((state) => state.counter.value) |
9 | | - const dispatch = useAppDispatch() |
| 9 | + const count = useAppSelector((state) => state.counter.value); |
| 10 | + const { isPaused } = useAppSelector((state) => state.status); |
| 11 | + const dispatch = useAppDispatch(); |
| 12 | + |
| 13 | + const handlePause = async () => { |
| 14 | + const store = await storePromise; |
| 15 | + store.pausePersist(); |
| 16 | + dispatch(setPaused(true)); |
| 17 | + }; |
| 18 | + |
| 19 | + const handleResume = async () => { |
| 20 | + const store = await storePromise; |
| 21 | + store.resumePersist(); |
| 22 | + dispatch(setPaused(false)); |
| 23 | + }; |
| 24 | + |
| 25 | + const handleClear = async () => { |
| 26 | + const store = await storePromise; |
| 27 | + await store.clearPersistedState(); |
| 28 | + // Reload to show the state has been reset from storage. |
| 29 | + window.location.reload(); |
| 30 | + }; |
10 | 31 |
|
11 | 32 | return ( |
12 | 33 | <> |
13 | | - <h1>count is {count}</h1> |
| 34 | + <h1>rtk-persist Example</h1> |
| 35 | + <div className="status-bar"> |
| 36 | + <p>Persistence Status: <strong>{isPaused ? 'Paused' : 'Active'}</strong></p> |
| 37 | + </div> |
| 38 | + |
| 39 | + <div className="card"> |
| 40 | + <h2>Counter Value: {count}</h2> |
| 41 | + <div className='button-group'> |
| 42 | + <button onClick={() => dispatch(counterSlice.actions.decrement())}> |
| 43 | + - |
| 44 | + </button> |
| 45 | + <button onClick={() => dispatch(counterSlice.actions.increment())}> |
| 46 | + + |
| 47 | + </button> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + |
14 | 51 | <div className="card"> |
15 | | - {/* <button onClick={() => dispatch(counterSlice.actions.decrement())}> |
16 | | - - |
17 | | - </button> |
18 | | - <button onClick={() => dispatch(counterSlice.actions.increment())}> |
19 | | - + |
20 | | - </button> */} |
21 | | - <button onClick={() => dispatch(decrement(1))}> |
22 | | - - |
23 | | - </button> |
24 | | - <button onClick={() => dispatch(increment(1))}> |
25 | | - + |
26 | | - </button> |
| 52 | + <h2>Persistence Controls</h2> |
| 53 | + <div className='button-group'> |
| 54 | + <button onClick={handlePause} disabled={isPaused}> |
| 55 | + Pause |
| 56 | + </button> |
| 57 | + <button onClick={handleResume} disabled={!isPaused}> |
| 58 | + Resume |
| 59 | + </button> |
| 60 | + <button onClick={handleClear} style={{ backgroundColor: '#c0392b' }}> |
| 61 | + Clear and Reload |
| 62 | + </button> |
| 63 | + </div> |
27 | 64 | </div> |
28 | 65 | </> |
29 | 66 | ) |
|
0 commit comments