Reactjs 为什么用react hooks测试库重新运行时只更新一次道具?
我有一个带有Reactjs 为什么用react hooks测试库重新运行时只更新一次道具?,reactjs,react-hooks,react-hooks-testing-library,Reactjs,React Hooks,React Hooks Testing Library,我有一个带有useffect的钩子。我注意到,useffect运行的次数不超过两次,因为在一次使用不同数据的rerender调用之后,后续调用不会获得更新的数据 export default function(lookForUsername) { const [dashboardHref, setDashboardHref] = useState(`https://www.example.com/`); useEffect(() => { // this is where
useffect
的钩子。我注意到,useffect
运行的次数不超过两次,因为在一次使用不同数据的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中的道具,而不是在以后的时间
注意
我已经更新了标题和问题的措辞,以更好地反映更多调试后的问题
更新
我过分简化了我的例子。我最初在这篇文章中只传递了一个论点,但问题是当我传递多个论点时,我将提供答案,但如果迈克·佩珀(Mike Peyper)发表文章,我会将他的观点标记为解决方案,因为他在上个月给了我 引述: 这不会像预期的那样工作,因为只传递了第一个参数 直到钩子回调。通常,你会通过一个对象与 多个键并在回调中对其进行分解: 这样做的原因是它应该模仿 包装组件的道具
不确定这是否按预期工作,或者是一个bug,因此直接向回购提出问题
const { rerender } = renderHook(({ a, b, c }) => useSomeHook(a, b, c))
rerender({ a, b, c})