Skip to content

Instantly share code, notes, and snippets.

@Ralexs0096
Created October 24, 2022 20:51
Show Gist options
  • Save Ralexs0096/43015aca73ac632bd706bf466f04be21 to your computer and use it in GitHub Desktop.
Save Ralexs0096/43015aca73ac632bd706bf466f04be21 to your computer and use it in GitHub Desktop.
react Context boilerplate
import { createContext, useReducer, useState } from 'react';
const myContext = createContext();
const reducerTest = (state, action) => {
switch (action.type) {
case 'CUALQUIER_NOMBRE':
return {
...state,
dataPbx: [], // aqui se filtra la data o algo asi
};
default:
return state;
}
};
const contextState = (props) => {
const [test, setTest] = useState('');
const [test2, setTest2] = useState('');
const initialState = {
dataTest: [],
dataTest2: [],
};
const [state, dispatch] = useReducer(reducerTest, initialState);
const getDataTest = () => {
dispatch({
action: 'CUALQUIER_NOMBRE',
payload: 'TEST',
});
};
return (
<myContext.Provider
value={{
test,
setTest,
test2,
setTest2,
getDataTest,
dataTest: state.dataTest,
}}
>
{props.children}
</myContext.Provider>
);
};
export default contextState;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment