💻
Bookstores
  • Tech Stacks
  • Django
    • Django REST framework
    • Serializer validation
    • Form validation in view class
  • Use Open API data model
  • Server Side react javascript
  • React context and redux difference
  • Migrate state management from Redux to Jotai
  • Migrate from REST to GraphQL
  • Migrate from REST to gRPC
  • How to handle cross origin?
Powered by GitBook
On this page
  • With Redux
  • With Jotai
  • Advanced Jotai

Was this helpful?

Migrate state management from Redux to Jotai

PreviousReact context and redux differenceNextMigrate from REST to GraphQL

Last updated 3 years ago

Was this helpful?

With Redux

There are really a lot of codes. Dig in each files to check the detail.

  • reducers

  • actions

  • component

      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 .

      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.

  •   import { atom } from "jotai";
    
      const initialCategoryListState = []
    
      export const initialCategoryState = {
          id: 0,
          name: "",
          status: ""
      }
    
      export const categoryListAtom = atom(initialCategoryListState)
      export const categoryAtom = atom(initialCategoryState)
  •   import { useAtom } from "jotai";
      import { categoryAtom, initialCategoryState } from "../../store/category";
    
      const Category = props => {
    
      const [category, setCategory] = useAtom(categoryAtom);
      ...
      }
  • import { Provider } from "jotai";
    
      <Provider store={store}>
        <App />
      </Provider>

Advanced Jotai

  • Use atom to load data. Move away the code to load from Component.

Define store

State in the component

Provider with store to wrap around .

rootReducer.js
category.js
category.js
category.js
App
category.js
Category.js
App