【发布时间】:2018-02-21 14:34:48
【问题描述】:
我创建了一个 Create-React-App 反应应用和 Redux。
我正在使用 connect 将我的 userReducer 映射到状态(通过 mapStateToPrope),即:
function mapStateToProps({ userProfile }) {
return { userProfile: userProfile ? userProfile : null };
}
export default connect(mapStateToProps, fetchUserProfile)(Profile);
我有一个输入,我已将 UserProfile Firstname 绑定到:
render(){
return (
<div>
<input type="text" value={this.props.userProfile.firstName} />
</div>
)
}
这个问题现在是我把它绑定到了道具上,我无法改变它,如果我输入输入它不会改变输入中的文本..
如果我添加一个 onChange={updateValue} 方法,并尝试更新道具,它将无法工作,因为组件无法更新自己的道具。
这样就剩下将道具转移到状态了。
constructor(props){
super(props);
this.state = { FirstName : this.props.userProfile.firstName }
}
这是从 props 中获取初始值的推荐方法吗?
【问题讨论】:
-
如果你已经在使用 redux,为什么不使用 store (redux)?
标签: reactjs react-redux