反应:当我尝试使用提供程序包装App组件时,无效的挂钩调用错误

时间:2020-06-05 07:04:19

标签: reactjs redux react-redux

我是React和Redux的新手。我尝试学习redux结构。我有商店我在 index.js 中添加了Provider组件 现在,我尝试用Provider包装App组件。然后将存储传递给index.js中的Provider,但是我得到一个典型的错误

错误:无效的挂钩调用。挂钩只能在功能组件的主体内部调用。可能由于以下原因之一而发生: 1.您的React和渲染器版本可能不匹配(例如React DOM) 2.您可能违反了《钩子规则》 3.您可能在同一应用程序中拥有多个React副本

我的index.js文件:

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import { createStore } from "redux";
import { Provider } from "react-redux";
import reducer from "./store/reducer";

const store = createStore(reducer);
ReactDOM.render(
    <Provider store={store}> //error show this line
        <App />
    </Provider>,
    document.getElementById("root")
);

serviceWorker.unregister();

另外,我的reducer.js

const initialState = {
    code: "initial", //my state which is using in component
};

const reducer = (state = initialState, action) => {
    return state;
};

export default reducer;

请帮助我。

0 个答案:

没有答案