Trung BìnhJavaScript iconJavaScript

Implement simple React context store (mini Redux)?

Mini Redux dùng Context + useReducer: store dùng chung cho toàn app mà không cần thư viện ngoài.

jsx
const StoreContext = createContext();
function StoreProvider({ children }) {
  const [state, dispatch] = useReducer(reducer, initialState);
  return (
    <StoreContext.Provider value={{ state, dispatch }}>
      {children}
    </StoreContext.Provider>
  );
}
const useStore = () => useContext(StoreContext);

Phỏng vấn thường hỏi để test hiểu state management internals.

Xem toàn bộ JavaScript cùng filter theo level & chủ đề con.

Mở danh sách JavaScript