Shashika Virajh
Shashika Virajh

Reputation: 9477

Enable button if the component values have been changed in react

I want to enable my react form, if the form values are different from the initial props values. My form is loaded through props and if the current state is different from props, I want to enable the button. How can I achieve this?

Upvotes: 2

Views: 1851

Answers (1)

Praveen Rao Chavan.G
Praveen Rao Chavan.G

Reputation: 2860

You can use a flag in your state like isEdited and have a handler to set this flag like

onEditHandler = () =>{
  setState((previousState, currentProps) => {
      if(this.state !== currentProps){
         return {isEdited:true};
      }else{
         return {isEdited:false};
      } 
  });
}

call this handler in onChange() handler of your inputs.

Use this flag in your button to enable and disable it

<button disable={this.state.isEdited}></button>

Upvotes: 1

Related Questions