简体   繁体   English

在 React 组件的 function 中访问新鲜的 REDUX state

[英]Access fresh REDUX state in function of React component

I'm using React and React-Redux-Toolkit and difficulties accessing the updated state from within a function.我正在使用 React 和 React-Redux-Toolkit,但很难从 function 中访问更新后的 state。

I have a custom React Component which I pass a variable of my state:我有一个自定义 React 组件,我传递了一个 state 的变量:

 const { useEffect, useState } = React; const ExternalComponent = ({ func }) => { func(); return ( <div></div> ); }; const Component = ({ state }) => { console.log("Component", state); const someFunction = () => { console.log("state", state); }; return ( <ExternalComponent func={someFunction} /> ); }; const Page = () => { const [state, setState] = useState(); useEffect(() => { setState(true); }, []); return ( <Component state={state} /> ); }; ReactDOM.render( <Page />, document.getElementById('root') );
 <body> <div id="root"></div> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script> <script type="text/babel" src="main.js"></script> </body>

On initial page render the component prints Component undefined , because state is yet undefined , which is understandable and not the problem.在初始页面呈现组件打印Component undefined ,因为state尚未undefined ,这是可以理解的,而不是问题所在。

When I update state by any means, the component re-renders and prints Component <the change> , which is also as expected.当我以任何方式更新state时,组件重新呈现并打印Component <the change> ,这也是预期的。

However, someFunction() will not update and still print the old state , which is still undefined .但是, someFunction()不会更新并且仍然打印旧的state ,它仍然是undefined

How do I get someFunction() to "update", ie print the updated state ?如何让someFunction()进行“更新”,即打印更新后的state

Edit: Typo编辑:错别字

Edit2: I created a code snippet in which everything works as expected. Edit2:我创建了一个代码片段,其中一切都按预期工作。 However, in my actual code I'm using PaypalButtons , where I pass someFunction to createOrder .但是,在我的实际代码中,我使用的是PaypalButtons ,我将someFunction传递给createOrder I'm not sure how PaypalButtons works and why someFunction would not get updated.我不确定 PaypalButtons 是如何工作的以及为什么someFunction不会更新。

Edit3: I'm using paypal-react-js Edit3:我正在使用paypal-react-js

This is happening because you have a stale value.发生这种情况是因为您有过时的价值。 Try using useCallback尝试使用useCallback

import { useCallback } from 'react';

const Component = ({ statefulVariable }) => {
  console.log("Component", statefulVariable);

  const someFunction = useCallback(() => {
    console.log(statefulVariable);
  }), [statefulVariable]);

  return (
    <SomeThirdPartyComponent callback={someFunction}/>
  );
};

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

相关问题 无法访问React组件中的Redux状态 - Unable to access Redux state in React component 我如何从另一个 React 组件访问 redux 状态? - How do i access redux state from another react component? 如何在 redux 辅助函数中引用反应组件的状态 - How to reference a react component's state in a redux helper function 通过在组件内部执行函数来响应 redux 状态的变化 - React to redux state changes by executing function inside of component React / Redux-组件状态未更新 - React/Redux - Component State not updating 组件中的空状态(对redux的反应) - Empty state in component ( react redux ) 将Redux状态传递给React组件 - Passing in Redux state to React Component 反应,执行组件的功能会导致Redux状态改变,从而导致组件卸载,函数此时是否返回? - React, executing component's function causes Redux state change which unmounts the component, does the function return at this point? 反应:redux-axios:如何访问一个组件可被另一组件访问的状态? - React: redux-axios: How to access the state of one component accessible to another component? React-redux:当它在功能组件中工作时,为什么我不能访问类组件中的状态属性? - React-redux: Why can't I access state properties in a class component, when it works in a functional component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM