在 useMemo 中使用 reducer 时使用 useState 和 useEffect 错误更新状态不起作用

时间:2021-06-12 13:49:57

标签: javascript reactjs react-native

我尝试更新状态中的数据,但返回错误 页面刷新时发生错误

const [local, setLocal] = React.useState(initialState || []);
React.useEffect(() => {
    getLocal();
}, []);
const getLocal = async () => {
    try {
        let lo = await _getData();
        if (lo != null) {
            setLocal(pre=>({...pre, ...lo});
        }
    } catch (err) {
        console.log(err);
    }
};
const [state, dispatch] = React.useReducer(reducer, local);
const store = React.useMemo(() => ({...state, dispatch}), [state]);

错误类型...

ERROR  Warning: React has detected a change in the order of 
Hooks called by App. This will lead to bugs and errors if not fixed.

   Previous render            Next render
   ------------------------------------------------------
1. useState                   useState
2. useMemo                    useEffect
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 个答案:

没有答案