2017-05-27 54 views
0

我有一个表,每行中有一个输入框。总共有3行,我需要从这三个输入框的值中计算总数。但是价值状态并未更新。我只获得最初的价值状态。例如,有代理,酒店,管理员的状态对象。如果我初始化代理值10,我在输入框中得到10,但是当我尝试更改值时,我只得到10。该值没有得到更新。状态值不更新

下面是代码

const Tbody = ({ roles, states, onChange, onBlur }) => { 
    const row = roles.map((role, index) => (
    <tr key={index}> 
     <td>{index + 1}</td> 
     <td>{role.label}</td> 
     <td> 
     <TextFieldGroup 
      id="formControlsText" 
      type="number" 
      name={role.name} 
      value={states[role.name]} 
      onChange={event => onChange(event)} 
      onBlur={event => onBlur(event)} 
      error={states.errors[role.name]} 
      required 
     /> 
     </td> 
    </tr> 
)); 
    return <tbody>{row}</tbody>; 
}; 

class Commission extends React.PureComponent { 
    state = { 
    agentCommission: 0, 
    hotelCommission: 0, 
    adminCommission: 0, 
    errors: {}, 
    isSubmitted: false 
    }; 

    handleChange = event => { 
    console.log(event.target); 
    const fieldName = event.target.name; 
    this.setState(
     { [event.target.name]: parseFloat(event.target.value) }, 
    () => { 
     this.validateField([fieldName]); 
     } 
    ); 
    }; 

    handleBlur = event => { 
    const fieldName = event.target.name; 
    this.validateField([fieldName]); 
    }; 

    validateField = validate => { 
    const errors = { ...this.state.errors }; 
    let hasError = false; 
    validate.forEach(field => { 
     if (
     parseFloat(this.state[field]) > 100 || 
     parseFloat(this.state[field]) < 0 
    ) { 
     hasError = true; 
     errors[field] = 'cannot be less than 0 and more than 100'; 
     } else { 
     errors[field] = ''; 
     } 
    }); 
    this.setState({ errors }); 
    return !hasError; 
    }; 

    render() { 
    const { agentCommission, adminCommission, hotelcommission } = this.state; 
    const totalCommission = agentCommission + adminCommission + hotelcommission; 
    console.log('totalCommission', totalCommission); 
    return (
     <div className="table-responsive"> 
     <table className="table table-striped table-bordered table-condensed"> 
      <thead> 
      <tr> 
       <th>S.N</th> 
       <th>Role</th> 
       <th>Commission</th> 
      </tr> 
      </thead> 
      <Tbody 
      roles={[ 
       { name: 'agentCommission', label: 'Agent' }, 
       { name: 'hotelCommission', label: 'Hotel Owner' }, 
       { name: 'adminCommission', label: 'Admin' } 
      ]} 
      states={{ ...this.state }} 
      onChange={this.handleChange} 
      onBlur={this.handleBlur} 
      /> 
      <tbody> 
      <tr> 
       <td> 
       <button 
        className="btn btn-primary" 
        onClick={this.handleSubmit} 
        disabled={totalCommission === 100 ? false : true}> 
        Save Changes 
       </button> 
       </td> 
      </tr> 
      </tbody> 
     </table> 
     </div> 
    ); 
    } 
} 
+0

我认为你的第一个问题是,国家应该在构造函数初始化。 –

+0

这是一个不属于标准的类属性特性(第2阶段提议),但语法有效 – milan

+0

当然,我们并不怀疑它在某些EcmaScript版本中的合法性,但React希望您在构造函数中初始化状态。你试过了吗? –

回答

1

在ReactJS,当你扩展阵营组件类,你必须在构造函数初始化状态。此外,您需要通过super(props)调用父类的构造函数。这是该阵营库的类可以访问你的状态值,以及在方法,如setState()

https://codepen.io/julianfresco/pen/ybrZNe/

class Commission extends React.PureComponent { 

    constructor(props, context) { 
    super(props) 
    this.state = { 
     agentCommission: 0, 
     hotelCommission: 0, 
     adminCommission: 0, 
     errors: {}, 
     isSubmitted: false 
    }; 

    // method instance bindings 
    this.handleChange = this.handleChange.bind(this) 
    this.handleBlur = this.handleBlur.bind(this) 
    this.validateField = this.validateField.bind(this) 
    } 

    // ... 

    // you had 1 typo in the render function, hotelCommission wasn't camel case 
    render() { 
    const { agentCommission, adminCommission, hotelCommission } = this.state; 
    // ... 
    } 
} 
+1

感谢您的帮助,哥们儿:) – milan

0
提供接入的唯一途径

的问题是Commission类,你在哪里没有初始化状态。 你的代码应该是这样的:

const Tbody = ({ roles, states, onChange, onBlur }) => { 
    const row = roles.map((role, index) => (
    <tr key={index}> 
     <td>{index + 1}</td> 
     <td>{role.label}</td> 
     <td> 
     <input 
      id="formControlsText" 
      type="number" 
      name={role.name} 
      value={states[role.name]} 
      onChange={event => onChange(event)} 
      onBlur={event => onBlur(event)} 
      error={states.errors[role.name]} 
      required 
     /> 
     </td> 
    </tr> 
)); 
    return <tbody>{row}</tbody>; 
}; 

class Commission extends React.PureComponent { 
    constructor(props) { 
    super(props) 
    this.state = { 
     agentCommission: 0, 
     hotelCommission: 0, 
     adminCommission: 0, 
     errors: {}, 
     isSubmitted: false 
    }; 
    } 


    handleChange(event) { 
    console.log(event.target); 
    const fieldName = event.target.name; 
    this.setState(
     { [event.target.name]: parseFloat(event.target.value) }, 
    () => { 
     this.validateField([fieldName]); 
     } 
    ); 
    }; 

    handleBlur(event) { 
    const fieldName = event.target.name; 
    this.validateField([fieldName]); 
    }; 

    validateField(validate) { 
    const errors = { ...this.state.errors }; 
    let hasError = false; 
    validate.forEach(field => { 
     if (
     parseFloat(this.state[field]) > 100 || 
     parseFloat(this.state[field]) < 0 
    ) { 
     hasError = true; 
     errors[field] = 'cannot be less than 0 and more than 100'; 
     } else { 
     errors[field] = ''; 
     } 
    }); 
    this.setState({ errors }); 
    return !hasError; 
    }; 

    render() { 
    const { agentCommission, adminCommission, hotelcommission } = this.state; 
    const totalCommission = agentCommission + adminCommission + hotelcommission; 
    console.log('totalCommission', totalCommission); 
    return (
     <div className="table-responsive"> 
     <table className="table table-striped table-bordered table-condensed"> 
      <thead> 
      <tr> 
       <th>S.N</th> 
       <th>Role</th> 
       <th>Commission</th> 
      </tr> 
      </thead> 
      <Tbody 
      roles={[ 
       { name: 'agentCommission', label: 'Agent' }, 
       { name: 'hotelCommission', label: 'Hotel Owner' }, 
       { name: 'adminCommission', label: 'Admin' } 
      ]} 
      states={{ ...this.state }} 
      onChange={this.handleChange} 
      onBlur={this.handleBlur} 
      /> 
      <tbody> 
      <tr> 
       <td> 
       <button 
        className="btn btn-primary" 
        onClick={this.handleSubmit} 
        disabled={totalCommission === 100 ? false : true}> 
        Save Changes 
       </button> 
       </td> 
      </tr> 
      </tbody> 
     </table> 
     </div> 
    ); 
    } 
} 

小提琴演示:https://codesandbox.io/s/KO3vDRGjR