Attaching Provider of react-redux gives me an invalid hook error

守給你的承諾、 提交于 2021-02-11 14:47:33

问题


index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

import { combineReducers, createStore } from 'redux'
import { Provider } from 'react-redux'
import productReducer from './reducers/product-reducers'
import userReducer from './reducers/user-reducer'

const allReducers = combineReducers({
    products: productReducer,
    user: userReducer
})

const store = createStore(
    allReducers,
    {
        products: [{name: 'iPhone'}],
        user: 'Michael'
    },
    window.devToolsExtension && window.devToolsExtension()
);

ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
serviceWorker.unregister();

App.js

import React from 'react';
import logo from './logo.svg';
import './App.css';

import { connect } from 'react-redux'

function App() {
  return (

    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

const mapStateToProps = state => {
  return state
}

export default connect(mapStateToProps)(App);

After I attached a provider to my react app, it keeps giving me the following error:

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

I don't think number 3 is an issue because I have checked through npm ls react that there is only one react installed.

How can I handle this issue?

来源:https://stackoverflow.com/questions/57088255/attaching-provider-of-react-redux-gives-me-an-invalid-hook-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!