【问题标题】:<Provider> is not available for module (external) hook<Provider> 不适用于模块(外部)挂钩
【发布时间】:2020-07-24 04:29:20
【问题描述】:

我已经创建了模块react-redux-area,但是模块文件中的hooks 不起作用。

在运行时我收到此错误: could not find react-redux context value; please ensure the component is wrapped in a &lt;Provider&gt;

我看不出与其他类似的外部挂钩有任何真正的不同,所以我在尝试解决这个问题。 非常感谢任何帮助和建议。


export const useMyArea = () => {
  const dispatch = useDispatch();             // <- No problem getting context here
  useDispatchActions(myAreaActions);          // <- This line DONT work!
  useCreateActionDispatchers(myAreaActions);  // <- This line work! (Another external hook)
  // Debugging why provider dont works for useDispatchActions!
  console.log(useDispatchActions.toString());
  console.log(useCreateActionDispatchers.toString());
  //const dispatchActions = useDispatchActions(myAreaActions);
  //useDispatchActions(myAreaActions);

  // This is the function that the useDispatchActions uses internally
  // Which work fine then the dispatch is add as argument
  const dispatchActions = CreateDispatchActions(dispatch, myAreaActions);


  const areaState = useSelector((state: IStoreState) => state.myArea);
  return {
    ...areaState,
    ...dispatchActions
  };
};

模块代码:

模块在 gtihub 上: https://github.com/alfnielsen/react-redux-area/blob/master/ReactReduxArea.ts

const useDispatchActions = <T extends MapMethodWithActionName<T>>(
   actionCreatorListObject: T
) => {
   const dispatch = useDispatch()
   const memoDispatchActionsObject = useMemo(
      () => CreateDispatchActions(dispatch, actionCreatorListObject),
      [dispatch, actionCreatorListObject]
   )
   return memoDispatchActionsObject
}

沙盒示例:

我有其他可以使用钩子的模块,在代码和框的示例中,我在应用程序钩子中使用它,没有问题。

(MyArea.ts 中的第 44 行不起作用,而相同类型的外部模块挂钩适用于第 45 行)

https://codesandbox.io/s/redux-area-base-ex-s5bg1?file=/src/MyArea.ts

【问题讨论】:

    标签: reactjs typescript redux react-redux react-hooks


    【解决方案1】:

    为什么它不起作用

    您捆绑了您的库,包括 react-redux(它在 package.json 的 dependencies 部分中列出),因此当此库用于某些应用程序时,该应用程序还包含 react-redux 作为依赖项,最终会得到两个不同的副本react-redux (它实际上取决于捆绑器配置)。 换句话说,react-redux-action-dispatchers-hook 中的 useDispatch 不是应用程序中使用的那个,因此它不会“看到”应用程序的提供者。

    查看示例中的堆栈跟踪:

    useReduxContext
    https://s5bg1.csb.app/node_modules/react-redux/7.2.0/es/hooks/useReduxContext.js:11:11
    
    useStore
    https://s5bg1.csb.app/node_modules/react-redux/7.2.0/es/hooks/useStore.js:17:28
    
    useDispatch
    https://s5bg1.csb.app/node_modules/react-redux/7.2.0/es/hooks/useDispatch.js:14:17
    
    useDispatchActions
    https://s5bg1.csb.app/node_modules/react-redux-area/out/ReactReduxArea.js:35:36
    

    useDispatchActions 使用来自react-redux 版本7.2.0 的useDispatch,而应用程序中列出的(参见packaje.json)是7.1.1

    如何解决

    不要在你的库中捆绑react-redux,它应该是peer dependency。

    【讨论】:

      【解决方案2】:

      我通过在包和项目中设置相同的版本来解决它。所以 npm 解决依赖关系,无需在包内安装 node_modules。 因此,如果您的项目 package.json 中有 "react-intl": "^5.10.9",请使用相同版本修复包 package.json。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-23
        • 1970-01-01
        • 2020-06-17
        • 2019-07-23
        • 1970-01-01
        • 2020-08-22
        相关资源
        最近更新 更多