【发布时间】:2020-02-02 07:17:49
【问题描述】:
我正在使用 Redux 进行状态管理。 我收到道具未定义的错误。 下面的块显示了我的代码,
index.js
ReactDOM.render(
<Provider store={configureStore()}>
<App />
</Provider>,
document.getElementById('root')
);
store.js
function configureStore(state = { rotating: true }) {
return createStore(rotateReducer,state);
}
浏览器控制台:
App {props: undefined, context: undefined, refs: {…}, updater: {…}}
on expand this App object in console,
props: {rotating: true, startAction: ƒ, stopAction: ƒ} (now props are defined).
App.js
function App() {
return (
<div className="App">
<header className="App-header">
<img
src={logo}
className={
"App-logo" +
(this.props.rotating ? "":" App-logo-paused")
}
alt="logo"
onClick={
this.props.rotating ?
this.props.stopAction : this.props.startAction
}
/>
<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>
);
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
App 组件加载的 props 有什么问题?
【问题讨论】:
-
能否也提供
App组件代码。 -
您在
App组件中定义mapStateToProps的位置? -
App 函数上方... const mapStateToProps = state => ({ ...state }); const mapDispatchToProps = dispatch => ({ startAction: () => dispatch(startAction), stopAction: () => dispatch(stopAction) });
标签: reactjs redux react-redux