const initialState = {
data: []
}
function reducer (state, action) {
switch(action.type) {
case 'case':
return (
axios.get('/api').then(
// res => console.log(res)
// res => console.log(res.data)
res => ({ data: res.data })
)
)
default:
return state
}
}
export default function Component() {
const [state, dispatch] = useReducer(reducer, initialState)
console.log(state)
console.log(state.data)
return (
<button onClick={() => dispatch({ type: 'case' })}>button</button>
)
}
In my code, what is my mistake? I get Promise {<pending>}
and undefined
after two line of console.log inside the Component. It seems state is not being updated before printing state
on console. How to fix that?