小编Moi*_*lzo的帖子

开玩笑地嘲笑 react-router-dom useHistory 和其他钩子

我有一个 CRA 和 Typescript (.tsx) 应用程序。我正在尝试为需要的组件编写测试useHistoryuseRouteMatch但它返回错误: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)

testing mocking reactjs jestjs react-router-dom

4
推荐指数
1
解决办法
2984
查看次数

标签 统计

jestjs ×1

mocking ×1

react-router-dom ×1

reactjs ×1

testing ×1