Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

How do you handle global state without Redux? Redux के बिना global state कैसे handle करें?

Answer
// Using Context API with useReducer
const StoreContext = React.createContext();

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

// Custom hook
function useStore() {
  return useContext(StoreContext);
}

// Usage
function Component() {
  const { state, dispatch } = useStore();
}
Context API + useReducer से Redux जैसा setup बना सकते हैं।

Was this answer clear?