React Hooks useState + useEffect + event给出陈旧状态 [英] React Hooks useState+useEffect+event gives stale state

查看:151
本文介绍了React Hooks useState + useEffect + event给出陈旧状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用React useEffect useState 的事件发射器,但它总是得到初始值状态而不是更新状态。如果我直接调用事件处理程序,即使使用 setTimeout ,它也可以工作。

I'm trying to use an event emitter with React useEffect and useState, but it always gets the initial state instead of the updated state. It works if I call the event handler directly, even with a setTimeout.

如果我将值传递给 useEffect()第二个参数,它会使它工作,但这会导致每次值改变时都会重新订阅事件发射器(由击键触发)。

If I pass the value to the useEffect() 2nd argument, it makes it work, however this causes a resubscription to the event emitter every time the value changes (which is triggered off of keystrokes).

我做错了什么?我试过 useState useRef useReducer ,以及 useCallback ,并且无法正常工作。

What am I doing wrong? I've tried useState, useRef, useReducer, and useCallback, and couldn't get any working.

这是一个复制品:

import React, { useState, useEffect } from "react";
import { Controlled as CodeMirror } from "react-codemirror2";
import "codemirror/lib/codemirror.css";
import EventEmitter from "events";

let ee = new EventEmitter();

const initialValue = "initial value";

function App(props) {
  const [value, setValue] = useState(initialValue);

  // Should get the latest value, both after the initial server load, and whenever the Codemirror input changes.
  const handleEvent = (msg, data) => {
    console.info("Value in event handler: ", value);
    // This line is only for demoing the problem. If we wanted to modify the DOM in this event, we would instead call some setState function and rerender in a React-friendly fashion.
    document.getElementById("result").innerHTML = value;
  };

  // Get value from server on component creation (mocked)
  useEffect(() => {
    setTimeout(() => {
      setValue("value from server");
    }, 1000);
  }, []);

  // Subscribe to events on component creation
  useEffect(() => {
    ee.on("some_event", handleEvent);
    return () => {
      ee.off(handleEvent);
    };
  }, []);

  return (
    <React.Fragment>
      <CodeMirror
        value={value}
        options={{ lineNumbers: true }}
        onBeforeChange={(editor, data, newValue) => {
          setValue(newValue);
        }}
      />
      {/* Everything below is only for demoing the problem. In reality the event would come from some other source external to this component. */}
      <button
        onClick={() => {
          ee.emit("some_event");
        }}
      >
        EventEmitter (doesnt work)
      </button>
      <div id="result" />
    </React.Fragment>
  );
}

export default App;

这是一个代码沙箱,在 App2

Here's a code sandbox with the same in App2:

https://codesandbox.io/s/ww2v80ww4l

App 组件有3种不同的实现--EventEmitter,pubsub-js和setTimeout。只有setTimeout有效。

App component has 3 different implementations - EventEmitter, pubsub-js, and setTimeout. Only setTimeout works.

谢谢!

编辑:为了澄清我的目标,我只想要 handleEvent 在所有情况下匹配Codemirror值。单击任何按钮时,应显示当前的codemirror值。而是显示初始值。

To clarify my goal, I simply want the value in handleEvent to match the Codemirror value in all cases. When any button is clicked, the current codemirror value should be displayed. Instead, the initial value is displayed.

推荐答案

过时了事件处理程序,因为它从定义它的闭包中获取其值。除非我们每次 value 更改时重新订阅新的事件处理程序,否则它将不会获得新值。

value is stale in the event handler because it gets its value from the closure where it was defined. Unless we re-subscribe a new event handler every time value changes, it will not get the new value.

解决方案1:为发布效果 [value] 创建第二个参数。这使得事件处理程序获得正确的值,但也会导致效果在每次击键时再次运行。

Solution 1: Make the second argument to the publish effect [value]. This makes the event handler get the correct value, but also causes the effect to run again on every keystroke.

解决方案2:使用 ref 将最新的存储在组件实例变量中。然后,创建一个效果,除了每次 value 状态更改时都会更新此变量。在事件处理程序中,使用 ref ,而不是

Solution 2: Use a ref to store the latest value in a component instance variable. Then, make an effect which does nothing but update this variable every time value state changes. In the event handler, use the ref, not value.

const [value, setValue] = useState(initialValue);
let refValue = useRef(value);
useEffect(() => {
    refValue.current = value;
});
const handleEvent = (msg, data) => {
    console.info("Value in event handler: ", refValue.current);
};

https://reactjs.org/docs/hooks-faq.html#what-c​​an-i -do-if-my-effect-dependencies - 经常更改

看起来该页面上还有一些其他解决方案也可能有效。非常感谢@Dinesh的帮助。

Looks like there are some other solutions on that page which might work too. Much thanks to @Dinesh for the assistance.

这篇关于React Hooks useState + useEffect + event给出陈旧状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆