Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

save previousdata react

You can make use of Redux to achieve the same. What you need to do is to save the form states in redux store rather than the component localState itself

You reducer will look something like

const initialState = {
      name: '',
            color: '',
            description: '',
            beacons: []
}
const BeaconForm = (state = initialState, action) => {
      switch(action.type) {
          case 'UPDATE_BEACON_FORM_VALUE': 
               return {
                    ...state, [action.newVal.key]: action.newVal.value
               }
          default: return state
      }
}

export default BeaconForm
And then have an action

export function updataFormValue(updatedVal) {
       return { type: 'UPDATE_BEACON_FORM_VALUE', newVal: updatedVal}
}
And in the compoennt

handleInputChange(event) {

    var data = {[event.target.name]: event.target.value}
    this.props.updataFormValue(data);

}
Apart from this you need to make your component Redux compatible with connect, mapStateToProps , etc which I assume you already know

And then instead of setting input value from state you will set it from props that youu get from redux store
 
PREVIOUS NEXT
Tagged: #save #previousdata #react
ADD COMMENT
Topic
Name
6+6 =