Author avatar

Gaurav Singhal

Mirror JSON Object Changes in the DOM in jQuery

Gaurav Singhal

  • Oct 14, 2020
  • 4 Min read
  • 50 Views
  • Oct 14, 2020
  • 4 Min read
  • 50 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

Creating user interfaces for web apps in React is a breeze. React gives developers flexibility and promotes reusability for maximum reuse of common UI elements such as buttons, text inputs, navigation, and so on. When it comes to development in React, you need not worry about how DOM changes happen as React does all the heavy-duty optimization for you.

State in React is used to keep track of how data changes over time in an app, and props are data that can be passed between different components. In this guide, you will learn how to mirror changes to a JSON object in the DOM.

Storing JSON in State

For the purposes of this guide, create a basic component with a user's info in state, as shown below. The user state is a JSON object that has name, age, and salary as properties.

1
2
3
4
5
6
7
state = {
  user: {
    name: "John Doe",
    age: 21,
    salary: 2000,
  },
};
js

In the render method, display the JSON object using the JSON.stringify() method. Below that, add three inputs, which will change the values of name, age, and salary respectively.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<div className="app">
  <pre>{JSON.stringify(user, null, 2)}</pre>
  <div>
    <input
      onChange={this.handleChange}
      name="name"
      placeholder="Name"
      value={user.name}
    />
    <input
      onChange={this.handleChange}
      name="age"
      placeholder="Age"
      value={user.age}
    />

    <input
      onChange={this.handleChange}
      name="salary"
      placeholder="Salary"
      value={user.salary}
    />
  </div>
</div>
jsx

Now, write a handleChange method that will change the user state value based on the value from the inputs. Make sure that the name of the inputs match the property names in the user state.

1
2
3
4
5
6
7
8
handleChange(e) {
    this.setState({
      user: {
        ...this.state.user,
        [e.target.name]: e.target.value,
      },
    });
  }
jsx

Putting all the above snippets together, your final component should look as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from "react";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      user: {
        name: "John Doe",
        age: 21,
        salary: 2000,
      },
    };

    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e) {
    this.setState({
      user: {
        ...this.state.user,
        [e.target.name]: e.target.value,
      },
    });
  }

  render() {
    const { user } = this.state;
    return (
      <div className="app">
        <pre>{JSON.stringify(user, null, 2)}</pre>
        <div>
          <input
            onChange={this.handleChange}
            name="name"
            placeholder="Name"
            value={user.name}
          />
          <input
            onChange={this.handleChange}
            name="age"
            placeholder="Age"
            value={user.age}
          />

          <input
            onChange={this.handleChange}
            name="salary"
            placeholder="Salary"
            value={user.salary}
          />
        </div>
      </div>
    );
  }
}
jsx

If you check out the results, you will notice that as you change the value in the textbox, the user JSON object also changes instantly.

In React, data is bound to an input using the value prop of an input element, which makes the input controlled. A controlled input takes the value from the state and triggers an onChange event when the value in the input changes. The component entirely controls the value of the inputs, and the inputs do not have direct access to the component's state. Hence, it is called one-way data binding.

Conclusion

If you are beginning with React, you must understand how one-way data binding works in React. It's ideal if you understand various strategies of passing data between components using props or context. Controlled components keep your UI in sync with the component state, making it predictable and easy to debug.

So that's it from this guide. To learn more about data binding in React, check out this article.

0