在道具中传递redux来作为未定义的道具?

在道具中传递redux来作为未定义的道具?,第1张

道具传递redux来作为未定义的道具?

首先,为了从商店获取更新状态,您需要

subscribe

const unsubscribe = store.subscribe(() => {    store.getState();})

因此,这不是应对变更的一种React方法。

其次, 当您将存储传递到

Layout
组件时,很有可能您没有
bind
layout
函数,因此
this.props.store
未定义。

为了使用Redux的反应方式,您可以使用Provider和

connect
方法

import { Provider } from 'react-redux';const renderAll = () => {    console.log("inside render all" ,store);    ReactDOM.render(      <Provider store={store}>          <App />     </Provider>,      document.getElementById('root')    );}

然后您的路线可以简单地

你可以

Body
Layout
喜欢调用组件

 <Body ChangeName={this.handleChangeName} s_key={this.state.inputkey} />

并像

const mapStateToProps = (state) => {   user: state.user}export default connect(mapStateToProps)(Body)

确保

connected Body component
在布局组件中使用。

之后,您可以

user state
Body
类似的组件中使用

{this.props.user.map(u => {       return  <UserDiv name={u.name} age={u.age} location={u.location} gender={u.gender} job={u.job} />;})}

如果最初在redux存储中未定义用户值,则需要在Body组件中添加一个检查,然后再使用它,例如

{this.props.user && this.props.user.map(u => {       return  <UserDiv name={u.name} age={u.age} location={u.location} gender={u.gender} job={u.job} />;})}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5140355.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-17
下一篇2022-11-17

发表评论

登录后才能评论

评论列表(0条)

    保存