Migrate state management from Redux to Jotai
With Redux
There are really a lot of codes. Dig in each files to check the detail.
reducers
actions category.js
component category.js
import { useSelector, useDispatch } from "react-redux"; import { setCategory, initCategory, setCategoryName, setCategoryStatus } from '../../actions/category'; const Category = props => { const { category, status } = useSelector(state => { return state.category }); const dispatch = useDispatch(); dispatch(...); dispatch(...); dispatch(...); ... }Provider with store to wrap around App.
import { Provider } from "react-redux"; <Provider store={store}> <App /> </Provider>
With Jotai
It's much simple. I only initialize the default state value, then just call with useAtom() Basically, both reducers and actions are not necessary any more.
Define store category.js
State in the component Category.js
Provider with store to wrap around App.
Advanced Jotai
Use atom to load data. Move away the code to load from Component.
Last updated
Was this helpful?