Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

reactjs - How can I delete previously passed object with key values pairs if I get passed a new object?

In my set state I am passing an object with key values pairs.

I have three scenarios when user can pass a new object to the state.

How can I delete previous entries and pass new ones?

Here I am passing a state object from Child component to parent

 <Car passInputs={this.getInputs}/>
 

This is how I get it in Parent component and pass it to the state

   getInputs = (data) => {
        this.setState({ input: { ...this.state.input, ...data} }, () => 
        {console.log(this.state.input)});
    }

My problem is that I conditionally pass 3 different objects and if I have passed first object conditionally then I need to delete it if I pass the second one and If I pass the third one I need to delete the second one. The sequence doesn't matter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this man

var List =[{title:'A1',id:1},{title:'A2',id:2},{title:'A3',id:3},{title:'A4',id:4},{title:'A5',id:5},{title:'A6',id:6},{title:'A7',id:7},]


getInputs = (data) => {
      var Obj= List  //take you previous state this.state.input
    Obj = Obj.filter(item=>item.id !== data.id)
    Obj = [...Obj,data]
      //  this.setState({ input: { ...this.state.input, ...data} }, () => 
        console.log(Obj,'New Updated List');
    }



getInputs({title:'AAA',id:4})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...