gpt4 book ai didi

reactjs - 类型错误 : Cannot read property ‘getState’ of undefined

转载 作者:行者123 更新时间:2023-12-03 13:44:01 27 4
gpt4 key购买 nike

我想尝试小的 redux 示例,所以我安装了 redux,但出现此错误

TypeError: Cannot read property ‘getState’ of undefined

new Provider webpack-internal:///./node_modules/react-redux/es/components/Provider.js:24:25



代码:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';

import './index.css';

const App = () => (<div>Hi </div>);

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

怎么了 ?

最佳答案

如果我们往里面看 react-redux <Provider />
<Provider />预计会提供 store支柱:

this.state = {
storeState: store.getState(),
^^here
store
}


因此错误。

但是,您可以使用 reducer 创建一个,应该类似于以下内容:
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import reducer from './reducer'
import App from './components/App'

const store = createStore(reducer)

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

关于reactjs - 类型错误 : Cannot read property ‘getState’ of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54847624/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com