简体   繁体   English

如何修复在 mocking 调度时有条件地调用 useDispatch REACT 挂钩

[英]How to fix useDispatch REACT hook being called conditionally when mocking dispatch

Error Message: React Hook "useDispatch" is called conditionally.错误消息:有条件地调用 React Hook“useDispatch”。 React Hooks must be called in the exact same order in every component render React Hooks 必须在每个组件渲染中以完全相同的顺序调用

I've been trying to figure out how to fix this for days, but nothing seeems to work.几天来我一直在想办法解决这个问题,但似乎没有任何效果。 The component works when I don't mock anything, but as soon as I mock dispatch it gives me this error.当我不模拟任何东西时,该组件可以工作,但是一旦我模拟分派,它就会给我这个错误。

Here's my component:这是我的组件:

import { Stage } from "../Stage/Stage";
import { useDispatch, useSelector } from "react-redux";
import { useEffect } from "react";
import { retrieveStageList } from "../../modules/reducer";
import { Process } from "../Process/Process";

export function RenderProcess({
  _useSelector = useSelector,
  _useDispatch = useDispatch(), //this is where it breaks
  _Process = Process,
}) {
  const dispatch = _useDispatch();
  const process = _useSelector((state) => state.renderProcess);
  const stageList = _useSelector((state) => state.stageList);
  useEffect(() => {
    if (process.processId !== null)
      dispatch(retrieveStageList(process.processId));
  }, []);

  return (
    <>
      <_Process process={process} />
      {stageList?.map((stageInputs, processId) => {
        return (
          <div key={processId}>
            <Stage stage={stageInputs} />
          </div>
        );
      })}
    </>
  );
}

Here's my test for this component:这是我对此组件的测试:

import { render } from "@testing-library/react";
import { RenderProcess } from "./RenderProcess";

test("should call dispatch once.", () => {
  const _useSelector = (fn) =>
    fn({
      stageList: [],
      renderProcess: { processId: "309624b6-9c96-4ba7-8f7e-78831614f685" },
    });
  
  const dispatch = jest.fn();
  render(
    <RenderProcess
      _useSelector={_useSelector}
      _useDispatch={() => dispatch}
      _Process={() => {}}
    />
  );
  expect(dispatch).toHaveBeenCalledTimes(1);
});

Any help on this would be amazing.对此的任何帮助都将是惊人的。

Found the fix, in the properties being passed in RenderProcess- on this在 RenderProcess 中传递的属性中找到了修复 - 在此

  • line: _useDispatch = useDispatch()行: _useDispatch = useDispatch()
  • it should be: _useDispatch = useDispatch它应该是: _useDispatch = useDispatch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在加载时调度 redux 挂钩 useDispatch(而不是单击按钮)? - How to dispatch the redux hook useDispatch on load (not on a button click)? 有条件地调用 React Hook “useEffect” - React Hook "useEffect" is called conditionally React Hook "useDispatch" 在 function "requestWithAction" 中被调用,它既不是 React function 组件也不是自定义 React Hook ZC1C425268E683854F14ZA7 - React Hook "useDispatch" is called in function "requestWithAction" that is neither a React function component nor a custom React Hook function “有条件地调用 React Hook”:如何检查组件中的可选上下文 - "React Hook is called conditionally": How to check for optional contexts in a component 有条件地调用 React Hook "React.useEffect" - React Hook "React.useEffect" is called conditionally react redux 为什么使用dispatch = useDispatch()? - react redux why use dispatch = useDispatch()? 如何修复 React Redux 和 React Hook useEffect 缺少依赖项:'dispatch' - How to fix React Redux and React Hook useEffect has a missing dependency: 'dispatch' 我在使用 React Hook 时遇到了问题 - useDispatch() - I got the problem in using React Hook - useDispatch() 使用dispatch()时未调用reducer - Reducer not being called when using dispatch() 自定义钩子 function 未在 React 中调用 - Custom hook function not being called in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM