简体   繁体   English

React Context 更新 state 在使用 Hooks 时没有反映在嵌套函数中

[英]React Context updated state not reflecting in nested functions when using Hooks

I have displayed two buttons during my initial render of the react component.我在最初渲染反应组件时显示了两个按钮。 If i clicked on button primary.如果我点击主要按钮。 the relevant onClick function will dispatch an event to update the reducer state and then executes the nested function which again prints state.相关的 onClick function 将发送一个事件来更新 reducer state,然后执行嵌套的 function 再次打印 state。

The problem I am facing was the updated state is not reflecting in the nested functions.我面临的问题是更新后的 state 没有反映在嵌套函数中。 Could you let me know what I am missing here?你能让我知道我在这里缺少什么吗?

import React, { useEffect, useContext } from "react";
import { MainContext } from "../Main/Main";

// Main
function FirstTab() {
  // // Access Context
  const { mainState, mainDispatch } = useContext(MainContext);
  const filterState = mainState.filter;
  console.log("AppMainState->SalesTab:", filterState);

  const updateFilterContext = (data) => {
    console.log("Inside Filter Update:", data);
    mainDispatch({ type: "UPDATE_FILTER", data: data });
  };

  //ss
  const PrimaryClick2 = (filterState) => {
    console.log("Primary Click2");
    console.log("AppMainState->PrimaryClick2:", filterState);
  };

  const PrimaryClick1 = (filterState) => {
    console.log("Primary Click1");
    console.log("AppMainState->PrimaryClick1:", filterState);
    PrimaryClick2(filterState);
  };

  const PrimaryClick = (filterState) => {
    console.log("Primary Click");
    console.log("AppMainState->PrimaryClick:", filterState);
    updateFilterContext({ selectedValue: "NEWVALUE" });
    PrimaryClick1(filterState);
  };

  useEffect(() => {
    console.log("Step1: Generating First Tab");
  }, [mainState.filter.applyFilter]);

  return (
    <div>
      <div>
        <button class="ui primary button" onClick={PrimaryClick}>
          Primary
        </button>
        <button class="ui secondary button">Secondary</button>
      </div>
    </div>
  );
}
export default FirstTab;

My guess is that mainDispatch is asynchronous.我的猜测是mainDispatch是异步的。

You might be able to check for changes with this:您也许可以通过以下方式检查更改:

useEffect(() => {
  console.log('mainState changed')
  console.log(mainState.filter)
}, [mainState])

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

相关问题 React 钩子 useCallback 不反映更新状态 - React hooks useCallback not reflecting updated state 无法使用反应钩子传递更新的状态 - Unable to pass updated state using react hooks 使用钩子做出反应时没有得到更新的 state 值? - not getting updated state value in react using hooks? React 组件从类更新为函数/挂钩但 state 不工作 - React component updated from classes to functions/hooks but state not working 有没有办法将 state 信息从反应挂钩传递到反应中的嵌套函数? - Is there a way of passing state information from react hooks into nested functions in react? 当我使用 React 钩子和 React 上下文添加新帖子时,为什么我的 state 会替换我的帖子 - Why is my state replacing my posts when I add a new one using React hooks and React context oksetState 函数未在反应挂钩中更新 - oksetState functions is not updated in react hooks 反应:更新 state 不反映在递归 useCallback - React: Updated state not reflecting in recursive useCallback 为什么我的 state 没有使用 React Hooks 更新? - Why is my state not being updated using React Hooks? 如何使用 React hooks 和可观察设计模式来保持 state 更新? - How to keep the state updated using React hooks and observable design pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM