我有一个 CRA 和 Typescript (.tsx) 应用程序。我正在尝试为需要的组件编写测试useHistory,useRouteMatch但它返回错误:TypeError: Cannot read property 'history' of undefined.
组件:
const SidebarTreeNav: React.FC<Props> = ( { tree, expanded } ) => {
const history = useHistory();
let { url } = useRouteMatch();
const handleClick = useCallback(
( path: string ) => history.push(`${ url }/${ path }`),
[ history ]);
return (
<SidebarTreeNavView expanded={ expanded ?? [ '0' ] }>
<SidebarTreeNavBlock handleClick={ handleClick } tree={ tree } node={ 0 } />
</SidebarTreeNavView> …Run Code Online (Sandbox Code Playgroud)