React组件状态与道具动态输入
React Components state vs props Dynamic Input
我已经用ReactJS和ES6做了几天的实验,并创建了几个组件,即<InputText />
、<InputNumber />
、<InputEmail />
,这些组件正在组件<ContactsEdit />
中使用。
尽管我读了很多教程,但我的子组件拒绝render()
和{this.state.Firstname}
,尽管我尝试了componentWillMount
、componentDidMount
、this.setState
、this.state
和this.forceUpdate()
,但它们会使this.props.Firstname
变得很好,这似乎真的很奇怪
我欢迎任何形式的建议或帮助。来源可以在github 上找到
谢谢:)
`导出默认类ContactsEdit扩展React.Component{
constructor(props) {
super(props);
this.contactId = this.props.params.id;
this.persistence = new Persistence('mock');
this.state = {
contact : {}
};
}
componentDidMount() {
this.persistence.getContactById( this.contactId ).then( (resolve) => {
this.setState({ contact : resolve.data });
this.data = resolve.data;
}).catch( (reject) => {
console.log(reject);
}) ;
this.forceUpdate();
}
onChange(id,newValue) {
this.state.contact[ id ] = newValue;
this.forceUpdate();
}
saveRecord( object ) {
console.log( this.state );
}
render() {
return (
<div className="ContactsEdit">
<h2>Contact Edit (Parent) id : {this.props.params.id}, FullName : {this.state.contact.Firstname} {this.state.contact.Lastname}</h2>
<div className="row">
<InputText id="Firstname" fieldName={this.state.contact.Firstname} labelName="Firstname" onChange={this.onChange.bind(this)} divClass="col-lg-3 col-md-3 col-sm-3 col-xs-6" />
<InputText id="Lastname" fieldName={this.state.contact.Lastname} labelName="Lastname" onChange={this.onChange.bind(this)} divClass="col-lg-3 col-md-3 col-sm-3 col-xs-6" />
<InputText id="SocSecId" fieldName={this.state.contact.SocSecId} labelName="SocSecId" onChange={this.onChange.bind(this)} divClass="col-lg-3 col-md-3 col-sm-3 col-xs-6" />
<InputText id="DriverLicId" fieldName={this.state.contact.DriverLicId} labelName="DriverLicId" onChange={this.onChange.bind(this)} divClass="col-lg-3 col-md-3 col-sm-3 col-xs-6" />
</div>
</div>
)
}
}
`
`导出默认类InputText扩展React.Component{
constructor(props) {
super(props);
this.state = { fieldName : this.props.fieldname};
}
componentWillMount() {
//this.state.fieldName = this.props.fieldname;
//this.forceUpdate();
}
updateState(evt) {
//this.setState( {fieldName : evt.target.value} );
this.props.onChange( evt.target.id, evt.target.value );
}
render() {
return (
<div className={this.props.divClass}>
<hr />
<label> {this.props.labelName} </label>
<div>{this.props.fieldName}</div>
<div>{this.state.fieldName}</div>
<input
type="text"
id={this.props.id}
value={this.props.fieldName}
onChange={this.updateState.bind(this)}
className="form-control"
/>
</div>
)
}
}`
this.props
直到运行之后才存在于构造函数中,将this
绑定到类的实例。使用从父级(在参数中)传入的props
constructor (props) {
super(props)
this.state = { fieldName: props.fieldname }
}
使用ES6类构造函数替换componentWillMount
此外,您不应直接修改this.state
。调用render()
不会引起反应。仅在构造函数中设置初始状态。其他地方请致电this.setState({ data: newData })
。
我发布这个主题和GitHub链接的原因是我已经尝试了一切。我知道使用this.state
不是最佳实践,我将其与this.forceUpdate
结合使用,因为this.setState()
不起作用。
我也知道componentWillUpdate()
已经在ES6中被替换了,但删除它并只使用constructor()
对我来说是行不通的
我也试过
constructor(props) {
super(props);
this.setState({ fieldName : 'something' })
// …
但这只会导致将值硬编码到我的组件中。我尝试过使用promise返回值:
constructor(props) {
super(props);
MyPromise( (resolve) => {
this.setState({ fieldName : resolve})
}
// …
但这也没有奏效。
所以我开始怀疑我的gulpfile是否有问题(因此提供了GitHub repo,这样有更多专业知识的人可能会检查并提供帮助)。
非常奇怪的是,尽管我知道this.props
解决方案不是最佳实践,但它仍然有效。
- 无法在 jquery 中的单击事件上生成动态输入字段
- 将动态输入表单数据保存到数据库kendoui中
- 如何从动态输入字段添加数字
- 带有动态输入的嵌套ng重复
- 如何正确地将动态输入字段传递到另一个页面进行显示
- 所有输入字段的动态输入长度
- 在AJAX中获取两个动态输入的值
- 访问ember中的动态输入值#ember中每个块
- 附加jQuery的后动态输入
- 在动态输入数据的下拉菜单中设置默认值
- Javascript - 验证 onSumbit 动态输入
- 动态输入文件中未加载任何数据
- 以 Meteor JS 形式存储动态输入值
- 将动态输入索引到其他动态输入数组
- 如何在创建动态输入文件时获取相同输入类型的单独 ID
- 如何编辑此动态输入文本区域 javascription
- 用于动态输入的JavaScript和HTML表单
- 使用 jquery 验证动态输入字段
- 使用验证器插件 Jquery 验证动态输入表单元素
- 从 jquery 动态输入字段获取值