【问题标题】:How to get return value from renderHook如何从 renderHook 获取返回值
【发布时间】:2019-12-25 08:22:10
【问题描述】:

我有一个返回函数的自定义钩子

export function useMyCustomHook = (dispatch) => () => {... some things with dispatching to store };

我正在尝试在开玩笑的测试中使用这样的返回值:

  const result = renderHook(() => useMyCustomHook(useDispatch()));
  console.log(result.current);

result.current 未定义。那么如何才能真正得到返回值呢?

【问题讨论】:

  • 你的钩子返回什么?
  • @AmitChauhan 钩子就在那里。我正在返回一个函数

标签: reactjs jestjs react-testing-library


【解决方案1】:

根据 react-hooks-testing-library renderHook 返回一个带有结果的对象,所以要访问它:

const { result } = renderHook(() => useMyCustomHook(useDispatch()));
console.log(result.current);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
相关资源
最近更新 更多