React Components state vs props 动态输入
React Components state vs props Dynamic Input
我已经用 ReactJS 和 ES6 进行了几天的试验,并创建了几个组件,即 <InputText />
、<InputNumber />
、<InputEmail />
,它们在组件 [=15] 中使用=].
这似乎真的很奇怪,尽管我已经阅读了很多教程,但我的子组件拒绝 render()
{this.state.Firstname}
,即使我尝试了 componentWillMount
、componentDidMount
、this.setState
、this.state
和 this.forceUpdate()
,但它们会很好地渲染 this.props.Firstname
我欢迎任何类型的建议或帮助。可以在 github
找到来源
谢谢 :)
`
导出默认 class 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>
)
}
}
`
`
导出默认 class 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
到 class 的实例之后才存在于构造函数中。使用从父级传入的 props
(在参数中)
constructor (props) {
super(props)
this.state = { fieldName: props.fieldname }
}
componentWillMount
被使用 ES6 class 构造函数替换
另外你不应该直接修改this.state
。调用 render()
不会引起反应。仅在构造函数中设置初始状态。在其他任何地方,调用 this.setState({ data: newData })
.
我发这个话题和GitHublink的原因是我什么都试过了。我知道使用 this.state
不是最佳实践,我将它与 this.forceUpdate
结合使用,因为 this.setState()
不起作用。
我也知道 componentWillUpdate()
已在 ES6 中被替换,但删除它并仅使用 constructor()
对我来说不起作用。
我也试过了
constructor(props) {
super(props);
this.setState({ fieldName : 'something' })
// …
但这只会导致将值硬编码到我的组件中。我尝试使用承诺返回值:
constructor(props) {
super(props);
MyPromise( (resolve) => {
this.setState({ fieldName : resolve})
}
// …
但这也不起作用。
所以我开始怀疑我的 gulpfile 是否有问题(因此提供了 GitHub 存储库以便有更多专业知识的人可以检查并提供帮助)。
但是,非常奇怪的是,this.props
解决方案有效,尽管我知道这不是最佳实践。
我已经用 ReactJS 和 ES6 进行了几天的试验,并创建了几个组件,即 <InputText />
、<InputNumber />
、<InputEmail />
,它们在组件 [=15] 中使用=].
这似乎真的很奇怪,尽管我已经阅读了很多教程,但我的子组件拒绝 render()
{this.state.Firstname}
,即使我尝试了 componentWillMount
、componentDidMount
、this.setState
、this.state
和 this.forceUpdate()
,但它们会很好地渲染 this.props.Firstname
我欢迎任何类型的建议或帮助。可以在 github
找到来源谢谢 :)
` 导出默认 class 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>
)
}
}
`
` 导出默认 class 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
到 class 的实例之后才存在于构造函数中。使用从父级传入的 props
(在参数中)
constructor (props) {
super(props)
this.state = { fieldName: props.fieldname }
}
componentWillMount
被使用 ES6 class 构造函数替换
另外你不应该直接修改this.state
。调用 render()
不会引起反应。仅在构造函数中设置初始状态。在其他任何地方,调用 this.setState({ data: newData })
.
我发这个话题和GitHublink的原因是我什么都试过了。我知道使用 this.state
不是最佳实践,我将它与 this.forceUpdate
结合使用,因为 this.setState()
不起作用。
我也知道 componentWillUpdate()
已在 ES6 中被替换,但删除它并仅使用 constructor()
对我来说不起作用。
我也试过了
constructor(props) {
super(props);
this.setState({ fieldName : 'something' })
// …
但这只会导致将值硬编码到我的组件中。我尝试使用承诺返回值:
constructor(props) {
super(props);
MyPromise( (resolve) => {
this.setState({ fieldName : resolve})
}
// …
但这也不起作用。
所以我开始怀疑我的 gulpfile 是否有问题(因此提供了 GitHub 存储库以便有更多专业知识的人可以检查并提供帮助)。
但是,非常奇怪的是,this.props
解决方案有效,尽管我知道这不是最佳实践。