I am using useReducer in my context provider. The idea is that I will be a central place for my state and dispatch functions to live.
I am making an axios call to a datapase to fetch projects. However, when I return in the dispatch function, it is returning a promise. How can I return the data from the axios call so that it stores the data from the call in state?
const initState = []
const projectsReducer = async (state, action) => {
switch(action.type) {
case 'FETCH_PROJECTS':
const req = await axios.get('/api/fetch_projects')
const { data } = req
return {...state, data}
default:
return state
}
}
useEffect(() => {
const initFetch = () => {
projectsDispatch({type: 'FETCH_PROJECTS'})
}
initFetch()
}, [])
const [projects, projectsDispatch] = useReducer(projectsReducer, initState)