Skip to content

Instantly share code, notes, and snippets.

@AlessandroAnnini
Created October 14, 2019 19:55
Show Gist options
  • Save AlessandroAnnini/c29b38a1c22eaebd5b5578828288dd6c to your computer and use it in GitHub Desktop.
Save AlessandroAnnini/c29b38a1c22eaebd5b5578828288dd6c to your computer and use it in GitHub Desktop.
Replace Redux with React Hooks, the easy way
import React, { useReducer, createContext } from "react";
export const Store = createContext();
export const createStoreProvider = ({ initialState, reducers }) => ({
children
}) => {
const reducer = (state, action) => reducers[action.type](state, action);
const [state, dispatch] = useReducer(reducer, initialState);
return (
<Store.Provider value={{ state, dispatch }}>{children}</Store.Provider>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment