比较第一个状态和最后一个状态reactjs

时间:2018-05-11 23:25:07

标签: reactjs state react-props

我正在尝试制作一个编辑页面。我更新了状态以进行任何更改。我想比较最初保存时的初始状态和最后一个状态。但我无法控制第一个州。

export default class extends Component {

    constructor(props){
        super(props);
        this.changeDetails = this.changeDetails.bind(this);
        this.state = {
            driver: this.props.driver
        }
    }

    changeDetails = value => {
        this.setState({
            driver:value
        })
    }

    onRegister = () => {
        //I want to make a comparison here.

    }


    render() { 
        const {driver} = this.state
        return (
            <div>
               <EditView driver={driver} changeDetails={this.changeDetails}/>
            </div>
        );
    }
}

EditView.js

 export default class extends Component {
      render() {
        const { driver} = this.props;

        const changeDetails = event => {
            driver['fname] = event.target.value;
            this.props.changeDetails(driver);
          };

        return (
          <div>
            <Input
                value={driver.fname}
                onChange={event => changeDetails(event)}
                />
          </div>
        );
      }
    }

1 个答案:

答案 0 :(得分:0)

不要直接改变驱动程序本身。使用这样的东西:

const changeDetails = event =>
    this.props.changeDetails( { ...driver, fname: event.target.value } );