EDIT (22 June 2020): as this question has some renewed interest, I realise there may be a few points of confusion. So I would like to highlight: the example in the question is intended as a toy example. It is not reflective of the problem. The problem which spurred this question, is in the use a third party library (over which there is limited control) that takes a callback as argument to a function. What is the correct way to provide that callback with the latest state. In react classes, this would be done through the use of this. In React hooks, due to the way state is encapsulated in the functions of React.useState(), if a callback gets the state through React.useState(), it will be stale (the value when the callback was setup). But if it sets the state, it will have access to the latest state through the passed argument. This means we can potentially get the latest state in such a callback with React hooks by setting the state to be the same as it was. This works, but is counter-intuitive.
——原问题继续如下——
我正在使用React钩子,并试图从回调中读取状态。每次回调访问它时,它都会回到默认值。
使用下面的代码。控制台会一直打印Count为:0无论我点击多少次。
function Card(title) {
const [count, setCount] = React.useState(0)
const [callbackSetup, setCallbackSetup] = React.useState(false)
function setupConsoleCallback(callback) {
console.log("Setting up callback")
setInterval(callback, 3000)
}
function clickHandler() {
setCount(count+1);
if (!callbackSetup) {
setupConsoleCallback(() => {console.log(`Count is: ${count}`)})
setCallbackSetup(true)
}
}
return (<div>
Active count {count} <br/>
<button onClick={clickHandler}>Increment</button>
</div>);
}
const el = document.querySelector("#root");
ReactDOM.render(<Card title='Example Component' />, el);
你可以在这里找到这个代码
我在回调中设置状态没有问题,只是在访问最新的状态。
如果要我猜的话,我认为任何状态的改变都会创建Card函数的一个新实例。这个回调是指向旧的那个。基于https://reactjs.org/docs/hooks-reference.html#functional-updates上的文档,我有一个想法,即在回调中调用setState,并将一个函数传递给setState,以查看是否可以从setState中访问当前状态。替换
setupConsoleCallback(() => {console.log(`Count is: ${count}`)})
with
setupConsoleCallback(() => {setCount(prevCount => {console.log(`Count is: ${prevCount}`); return prevCount})})
你可以在这里找到这个代码
这种方法也没有奏效。
编辑:实际上第二种方法是可行的。我刚才回试的时候打错了。这是正确的做法。我需要调用setState来访问之前的状态。即使我无意设置状态。
我觉得我对React类采取了类似的方法,但是。为了代码的一致性,我需要坚持使用React Effects。
如何从回调中访问最新的状态信息?
不要尝试在回调中访问最近的状态,而是使用useEffect。使用setState返回的函数设置状态不会立即更新您的值。状态更新是批处理和更新的
如果您将useEffect()看作是setState的第二个参数(来自基于类的组件),这可能会有所帮助。
如果你想用最近的状态做一个操作,使用useEffect(),当状态改变时,它会被击中:
const {
useState,
useEffect
} = React;
function App() {
const [count, setCount] = useState(0);
const decrement = () => setCount(count-1);
const increment = () => setCount(count+1);
useEffect(() => {
console.log("useEffect", count);
}, [count]);
console.log("render", count);
return (
<div className="App">
<p>{count}</p>
<button onClick={decrement}>-</button>
<button onClick={increment}>+</button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render( < App / > , rootElement);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>
<div id="root"></div>
更新
你可以为setInterval创建一个钩子,并像这样调用它:
const {
useState,
useEffect,
useRef
} = React;
function useInterval(callback, delay) {
const savedCallback = useRef();
// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current();
}
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
}
function Card(title) {
const [count, setCount] = useState(0);
const callbackFunction = () => {
console.log(count);
};
useInterval(callbackFunction, 3000);
useEffect(()=>{
console.log('Count has been updated!');
}, [count]);
return (<div>
Active count {count} <br/>
<button onClick={()=>setCount(count+1)}>Increment</button>
</div>);
}
const el = document.querySelector("#root");
ReactDOM.render(<Card title='Example Component'/>, el);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>
<div id="root"></div>
关于useEffect()的更多信息
我真的很喜欢@davnicwil的回答,希望有了useState的源代码,他的意思可能会更清楚。
// once when the component is mounted
constructor(initialValue) {
this.args = Object.freeze([initialValue, this.updater]);
}
// every time the Component is invoked
update() {
return this.args
}
// every time the setState is invoked
updater(value) {
this.args = Object.freeze([value, this.updater]);
this.el.update()
}
在用法中,如果initialValue以数字或字符串开头,例如1。
const Component = () => {
const [state, setState] = useState(initialValue)
}
预排
第一次运行useState,这个。Args = [1,]
你第二次运行useState,这个。Args没有变化
如果setState被2调用,这个。Args = [2,]
下次你运行useState时,这个。Args没有变化
现在,如果你做一些事情,特别是对值的延迟使用。
function doSomething(v) {
// I need to wait for 10 seconds
// within this period of time
// UI has refreshed multiple times
// I'm in step 4)
console.log(v)
}
// Invoke this function in step 1)
doSomething(value)
您将得到一个“旧”值,因为您首先将当前副本(当时)传递给它。虽然这。Args每次都会获得最新的副本,这并不意味着旧的副本会被更改。您传递的值不是基于引用的。这可以成为一个特色!!
总结
为了改变它,
使用该值而不传递它;
使用对象作为值;
使用useRef获取最新值;
或者设计另一个钩子。
虽然上述方法都可以修复它(在其他答案中),但问题的根本原因是您将旧值传递给函数,并期望它与未来值一起运行。我认为这是它一开始出错的地方,如果你只看解就不太清楚。