我正在使用next-routes
并在我的 React 组件中使用下面的useEffect
代码块来检测Router
更改事件:
useEffect(() => {
// THIS BLOCK WILL BE EXECUTED WHEN THE COMPONENT IS ALREADY MOUNTED AND SCREEN WOULD BE REDNDERED IN WHEN ROUTED CLIENT SIDE
const handleRouterChangeComplete = url => {
// console.log('INNNN handleRouterChangeComplete url = ', url);
// MY REST OF THE LOGIC HERE
};
// THIS BLOCK WILL BE EXECUTED WHEN THE SCREEN WOULD BE REDNDERED IN WHEN ROUTED CLIENT SIDE
Router.events.on('routeChangeComplete', handleRouterChangeComplete);
return () => {
Router.events.off('routeChangeComplete', handleRouterChangeComplete);
};
}, []);
我正在jest
为此组件编写单元测试用例并面临以下错误:
TypeError: Cannot read property 'on' of undefined
183 |
184 | // THIS BLOCK WILL BE EXECUTED WHEN THE SCREEN WOULD BE REDNDERED IN WHEN ROUTED CLIENT SIDE
> 185 | Router.events.on('routeChangeComplete', handleRouterChangeComplete);
有人可以帮我嘲笑这个Router Change
事件吗?谢谢