const [state, setState] = useState<State>({
data: [],
loading: true
});
const dispatch = useDispatch<Dispatch<UIAction>>();
useEffect(() => {
dispatch({
type: SET_LOADING,
loadbarShowing: true
})
}, [dispatch])
useEffect(() => {
const invoke = async () => {
// Doing an effect here, including setting the state (including loading: false)
};
invoke();
}, [/* ... */]); // Even IF i pass an empty array here, it's still being invoked exactly twice.
上面的代码以某种方式导致 useEffect() 被调用两次。如果我不在第一个 useEffect 中调用 dispatch()
,则第二个效果很好。
这可能是什么原因?