我一直在試驗ReactJS和ES6幾天和創建的耦合部件即<InputText />
,<InputNumber />
,<InputEmail />
,即在組件<ContactsEdit />
被使用。反應的組分狀態VS道具動態輸入
看來真是奇怪,即使我讀了很多教程我的子組件拒絕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>
)
}
} `
歡迎來到Stack!只要在你的問題中發佈相關的代碼,這將(希望)刪除downvotes並得到你需要的答案。 – azium
謝謝:)我會這樣做:) –