为什么 re-run 只用 react-hooks-testing-libary 更新道具一次?
Why is re-run only updating props one time with react-hooks-testing-libary?
我对 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);
我不明白为什么 re-render 使用不同的值会在第一次更新 renderHook 中的 props,但随后的时间不会。
注意
我更新了标题和问题的措辞,以便在更多调试后更好地反映问题
更新
我over-simplified我的榜样。我本来只在这个 post 中传递一个参数,但问题是当我传递多个参数时
将提供答案,但如果 Mike Peyper 发帖,我会将他的标记为解决方案,因为他在 https://github.com/mpeyper/react-hooks-testing-library/issues/75.
上给我了
引用:
This wont work as expected as only the first argument is passed
through to the hook callback. Usually you would pass an object with
multiple keys and destructure it in the callback:
const { rerender } = renderHook(({ a, b, c }) => useSomeHook(a, b, c))
rerender({ a, b, c})
The reason for this is it's supposed to mimic the
props of the wrapping component.
我对 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);
我不明白为什么 re-render 使用不同的值会在第一次更新 renderHook 中的 props,但随后的时间不会。
注意 我更新了标题和问题的措辞,以便在更多调试后更好地反映问题
更新 我over-simplified我的榜样。我本来只在这个 post 中传递一个参数,但问题是当我传递多个参数时
将提供答案,但如果 Mike Peyper 发帖,我会将他的标记为解决方案,因为他在 https://github.com/mpeyper/react-hooks-testing-library/issues/75.
上给我了引用:
This wont work as expected as only the first argument is passed through to the hook callback. Usually you would pass an object with multiple keys and destructure it in the callback:
const { rerender } = renderHook(({ a, b, c }) => useSomeHook(a, b, c))
rerender({ a, b, c})
The reason for this is it's supposed to mimic the props of the wrapping component.