我有一个钩子useEffect
。我注意到它useEffect
没有运行超过两次,因为在rerender
使用不同数据进行一次调用后,后续调用不会获得更新的数据。
export default function(lookForUsername) {
const [dashboardHref, setDashboardHref] = useState(`https://www.example.com/`);
useEffect(() => {
// this is where I have code that i want to
// run on re-render, but it won't because it stops getting updated
}, [lookForUsername]);
return [dashboardHref];
}
我的测试发生了这种情况
// this will log false, false on first run
const { rerender, waitForNextUpdate } = renderHook((lookForUsername=false, otherOption=false) => {
console.log(lookForUsername, otherOption);
return useMyHook(lookForUsername);
});
console.log('first re-render');
// this will make the console say true, false
rerender(true, true);
console.log('second re-render');
// console will still say true, false
rerender(false, false);
console.log('third re-render');
// console will stay true, false (so it's not just one behind)
rerender(true, true);
我不明白为什么用不同的值重新渲染会在第一次更新 renderHook 中的道具,但不是随后的时间。
注意 我已经更新了标题和问题的措辞,以便在更多调试后更好地反映问题
更新 我过度简化了我的例子。我最初在这篇文章中只传递了一个参数,但问题是当我传递多个参数时