How can I remove an attribute from a Reactjs component's state object

后端 未结 10 2042
南笙
南笙 2020-12-06 09:17

If I have a react component that had a property set on it\'s state:

onClick() {
    this.setState({ foo: \'bar\' });
}

Is it possible to re

10条回答
  •  独厮守ぢ
    2020-12-06 09:50

    You can use Object.assign to make a shallow copy of your application's state at the correct depth and delete the element from your copy. Then use setState to merge your modified copy back into the application's state.

    This isn't a perfect solution. Copying an entire object like this could lead to performance / memory problems. Object.assign's shallow copy helps to alleviate the memory / performance concerns, but you also need to be aware of which parts of your new object are copies and which parts are references to data in the application state.

    In the example below, modifying the ingredients array would actually modify the application state directly.

    Setting the value of the undesired element to null or undefined doesn't remove it.

    const Component = React.Component;
    
    class App extends Component {
      constructor(props) {
        super(props);
        this.state = {
          "recipes": {
            "1": {
              "id": 1,
              "name": "Pumpkin Pie",
              "ingredients": [
                "Pumpkin Puree",
                "Sweetened Condensed Milk",
                "Eggs",
                "Pumpkin Pie Spice",
                "Pie Crust"
              ]
            },
            "2": {
              "id": 2,
              "name": "Spaghetti",
              "ingredients": [
                "Noodles",
                "Tomato Sauce",
                "(Optional) Meatballs"
              ]
            },
            "3": {
              "id": 3,
              "name": "Onion Pie",
              "ingredients": [
                "Onion",
                "Pie Crust",
                "Chicken Soup Stock"
              ]
            },
            "4": {
              "id": 4,
              "name": "Chicken Noodle Soup",
              "ingredients": [
                "Chicken",
                "Noodles",
                "Chicken Stock"
              ]
            }
          },
          "activeRecipe": "4",
          "warningAction": {
            "name": "Delete Chicken Noodle Soup",
            "description": "delete the recipe for Chicken Noodle Soup"
          }
        };
        
        this.renderRecipes = this.renderRecipes.bind(this);
        this.deleteRecipe = this.deleteRecipe.bind(this);
      }
      
      deleteRecipe(e) {
        const recipes = Object.assign({}, this.state.recipes);
        const id = e.currentTarget.dataset.id;
        delete recipes[id];
        this.setState({ recipes });
      }
      
      renderRecipes() {
        const recipes = [];
        for (const id in this.state.recipes) {
          recipes.push((
            
              
                
              
              {this.state.recipes[id].name}
            
          ));
        }
        return recipes;
      }
                    
      render() {
        return (
          
            {this.renderRecipes()}
          
    ); } } ReactDOM.render(, document.getElementById('app'));
    
    
    

提交回复
热议问题