简体   繁体   English

无法读取上下文提供程序中 useReducer 钩子更新的状态

[英]Unable to read state updated by useReducer hook in context provider

I am using useReducer hook to manage my state, but it seems like I have a problem with reading updated state in my context provider.我正在使用useReducer钩子来管理我的状态,但在我的上下文提供程序中读取更新状态似乎有问题。

My context provider is responsible to fetch some remote data and update the state based on responses:我的上下文提供程序负责获取一些远程数据并根据响应更新状态:

import React, { useEffect } from 'react';
import useAppState from './useAppState';


export const AppContext = React.createContext();

const AppContextProvider = props => {
  const [state, dispatch] = useAppState();

  const initialFunction = () => {
    fetch('/some_path')
      .then(res => {
        dispatch({ type: 'UPDATE_STATE', res });
      });
  };

  const otherFunction = () => {
    fetch('/other_path')
      .then(res => {
        // why is `state.stateUpdated` here still 'false'????
        dispatch({ type: 'DO_SOMETHING_ELSE', res });
      });
    }
  };

  const actions = { initialFunction, otherFunction };

  useEffect(() => {
    initialFunction();
    setInterval(otherFunction, 30000);
  }, []);

  return (
    <AppContext.Provider value={{ state, actions }}>
      {props.children}
    </AppContext.Provider>
  )
};

export default AppContextProvider;

and useAppState.js is very simple as: useAppState.js非常简单,如下所示:

import { useReducer } from 'react';


const useAppState = () => {
  const reducer = (state, action) => {
    switch (action.type) {
      case 'UPDATE_STATE':
        return {
          ...state,
          stateUpdated: true,
        };
      case 'DO_SOMETHING_ELSE':
        return {
          ...state,
          // whatever else
        };
      default:
        throw new Error();
    }
  };


  const initialState = { stateUpdated: false };

  return useReducer(reducer, initialState);
};


export default useAppState;

The question is, as stated in the comment above, why is state.stateUpdated in context provider's otherFunction still false and how could I access state with latest changes in the same function?现在的问题是,如上面的评论说,为什么state.stateUpdated在上下文提供的otherFunction还是false ,我怎么能访问相同的功能与最新的变化状态?

state will never change in that function该函数中的state永远不会改变

The reason state will never change in that function is that state is only updated on re-render.究其原因state将永远不会在该函数的变化是, state只更新重新渲染。 Therefore, if you want to access state you have two options:因此,如果您想访问state您有两个选择:

  1. useRef to see a future value of state (you'll have to modify your reducer to make this work) useRef查看state的未来值(您必须修改您的减速器才能使其工作)
const updatedState = useRef(initialState);
const reducer = (state, action) => {
  let result;
  // Do your switch but don't return, just modify result

  updatedState.current = result;
  return result;
};

return [...useReducer(reducer, initialState), updatedState];
  1. You could reset your setInterval after every state change so that it would see the most up-to-date state.您可以在每次状态更改后重置setInterval ,以便它看到最新的状态。 However, this means that your interval could get interrupted a lot.但是,这意味着您的间隔可能会中断很多。
const otherFunction = useCallback(() => {
  fetch('/other_path')
    .then(res => {
      // why is `state.stateUpdated` here still 'false'????
      dispatch({ type: 'DO_SOMETHING_ELSE', res });
    });
  }
}, [state.stateUpdated]);

useEffect(() => {
  const id = setInterval(otherFunction, 30000);
  return () => clearInterval(id);
}, [otherFunction]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 挂钩更改 state 不会更新上下文提供程序的值? - hook change state doesn't update context provider's value? useReducer 返回 promise 而不是更新的 state - useReducer returning a promise instead of the updated state 使用 React useState 钩子时无法获取更新的 state - Unable to get the updated state when using React useState hook 无法从 setTimeout 中定义的函数中的 useReducer 钩子访问更新的数据 - Cannot access updated data from useReducer hook in function defined in setTimeout 使用带有 useReducer() 钩子的 React Context API 的优缺点是什么? - What are the pros and cons on using React Context API with the useReducer() hook? 为什么在 useReducer 钩子中使用 switch 语句来管理状态? - Why is switch statement used in useReducer hook to manage state? 从useReducer Hook返回的状态是“深度复制”还是reducers输出的引用? - Is the state returned from the useReducer Hook a “deep copy” or a reference of a reducers output? 如何在单个提供程序中为多个 state 对象实现 React useReducer - How to implement React useReducer for multiple state objects in a single provider React useReducer 不更新 React 上下文中的状态 - React useReducer don't update state in React context State 没有被 React 的 state 钩子更新? - State not being updated with React's state hook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM