Created
October 24, 2022 20:51
-
-
Save Ralexs0096/43015aca73ac632bd706bf466f04be21 to your computer and use it in GitHub Desktop.
react Context boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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