get renderHook's result more than once

[亡魂溺海] 提交于 2020-01-16 09:07:10

问题


I got a test which calls renderHook like this:

const { result } = renderHook(() => useMyHook(useDispatch()), wrapper);

It's all good and working, and I can get the return value in result.current..

It seems like I can't call another hook with renderHook.. If I do the exact same thing, then it says that result is already defined. If I change the name to something else, like "res", it says that res is undefined, like it's only working for the specific name "result".

So how can I call it more than one time?


回答1:


Since you should only be testing one hook per test, perhaps you have to unmount the current component first:

const { result, unmount } = renderHook(() => useMyHook(useDispatch()), wrapper);

// ... do what you need with result

unmount();

// call your new hook...
const { result } = renderHook(() => useNewHook(useDispatch()), wrapper);


来源:https://stackoverflow.com/questions/59478806/get-renderhooks-result-more-than-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!