immutability helper

Solutions on MaxInterview for immutability helper by the best coders in the world

showing results for - "immutability helper"
Antonio
29 Nov 2019
1import React, { Component } from "react";
2import "./styles.css";
3import update from "immutability-helper";
4
5class App extends Component {
6  state = {
7    pizzas: {
8      margherita: {
9        toppings: ["tomato sauce", "mozzarella cheese"],
10        prices: [{ small: "5.00" }, { medium: "6.00" }, { large: "7.00" }]
11      },
12      prosciutto: {
13        toppings: ["tomato sauce", "mozzarella cheese", "ham"],
14        prices: {
15          small: "6.50",
16          medium: "7.50",
17          large: "8.50"
18        }
19      }
20    },
21    person: {
22      name: "ssrk bhai"
23    }
24  };
25
26  sayHello = () => {
27    this.setState(
28      {
29        pizzas: update(this.state.pizzas, {
30          margherita: { prices: { $push: [{ ghana: 45 }] } }
31        })
32      },
33      () => {
34        console.log(this.state);
35      }
36    );
37  };
38
39  render() {
40    return (
41      <div className="App">
42        <h1>Hello CodeSandbox</h1>
43        <h2>Start editing to see some magic happen!</h2>
44        <button onClick={this.sayHello}>Click me!</button>
45      </div>
46    );
47  }
48}
49
50export default App;
51