栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在道具中传递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} />;})}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/440334.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号