【问题标题】:React Components state vs props Dynamic InputReact 组件状态与道具动态输入
【发布时间】:2016-04-03 10:55:48
【问题描述】:

我已经用 ReactJS 和 ES6 进行了几天的试验,并创建了几个组件,即 <InputText /><InputNumber /><InputEmail />,它们正在组件 <ContactsEdit /> 中使用。

尽管我已经阅读了很多教程,但我的子组件拒绝 render(){this.state.Firstname} 似乎真的很奇怪,即使我尝试了 componentWillMountcomponentDidMountthis.setStatethis.statethis.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>
    )
}

} `

【问题讨论】:

  • 欢迎来到堆栈!只需在您的问题中发布相关代码,这将(希望)消除反对意见并为您提供所需的答案。
  • 谢谢 :) 我会这样做的 :)

标签: javascript reactjs react-jsx


【解决方案1】:

this.props 在构造函数运行之后才存在于构造函数中,将this 绑定到类的实例。使用从父级传入的props(在参数中)

constructor (props) {
    super(props)
    this.state = { fieldName: props.fieldname }
}

componentWillMount 使用 ES6 类构造函数替换

你也不应该直接修改this.state。调用render() 不会引起反应。仅在构造函数中设置初始状态。其他任何地方,请致电this.setState({ data: newData })

【讨论】:

    【解决方案2】:

    我发布主题和 GitHub 链接的原因是我已经尝试了所有方法。我知道使用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 解决方案有效,尽管我知道这不是最佳做法。

    【讨论】:

      猜你喜欢
      • 2019-08-30
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2017-11-28
      • 2017-05-18
      • 1970-01-01
      相关资源
      最近更新 更多