1

I am having multiple locations, on check of the particular location i can able to push those into array on check of checkbox unable to remove from array

handleChange(e){
    if(e.target.checked){
        let selectedLocations = this.state.selectedLocations;
        selectedLocations.push({
            name: e.target.name,
            isActive: true
        });
        this.setState({
            selectedLocations: selectedLocations
        });
    }else{
        let selectedLocations = this.state.selectedLocations;

    }


}

{
 locations && locations.map((el, i) => {
   return (
         <div className="col-md-4">
         <div className="form-group">
         <input type="checkbox" value="true"  name={el.name} onChange={this.handleChange.bind(this)}/><label> &nbsp;{el.name} </label>
      </div>
  </div>
 )
})
}

I want to remove from array after unchecking particular location

1
  • you have set a state in else condition, since you are using same selectedLocations ur result gets unchanged, also you need to consider locations should be updated in sync with selectedLocations, I assume because you have not posted the complete code. Commented Sep 25, 2019 at 11:55

4 Answers 4

1

You shouldn't try to mutate the state by pushing directly to state. I would suggest try something similar like below

handleChange(e){
    if(e.target.checked){
        this.setState(({selectedLocations}) => ({
            selectedLocations: [...selectedLocations, {
            name: e.target.name,
            isActive: true
        }]
        }));
    } else {
        this.setState(({selectedLocations}) => ({
            selectedLocations: selectedLocations.filter((element) =>  (e.target.name !== element.name))
        }));
    }
}

Sign up to request clarification or add additional context in comments.

1 Comment

this is what my answer would be, except there are just a couple of issues (which I assume are just from overlooking when copy pasting): the const selectedLocations... will not be available in the else block. Also, I believe it would be better practice to use the callback syntax inside setState instead of setting a const from state. (that is: this.setState(({selectedLocations}) => ({ selectedLocations:selectedLocations.filter((element) => (e.target.name !== element.name))})); and same idea for the first setState )
0

Try to pass map index with function and split that index value from an array.

Comments

0

You can use .filter(). I'm assuming that the name property is unique but you can also store a key to be more precise:

this.setState({
  selectedLocations: selectedLocations.filter(l => l.name !== e.target.name)
});

1 Comment

@Nathanael, I think answer should be updated to l.name !== e.target.name. The person who downvoted didn't bother to correct you I believe :).
0

Use lodash remove function

handleCheckboxChange = (e, id) => {
  const { selected_ids } = this.props;
  let updated_selected_ids = [];
  if (e.target.checked) {
    updated_selected_ids = selected_ids.concat([id]);
  } else {
    updated_selected_ids = remove(selected_ids, n => n !== id);
  }
}

If you don't want to use lodash you can use filter method as well.

handleCheckboxChange = (e, id) => {
  const { selected_ids } = this.props;
  let updated_selected_ids = [];
  if (e.target.checked) {
   updated_selected_ids = selected_ids.concat([id]);
 } else {
   updated_selected_ids = selected_ids.filter(function(n){ return n !== id })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.