The React way of dynamically updating a website’s content is different from Vanilla JavaScript. In JavaScript, we do it by directly manipulating DOM, whereas in React, we have a feature called useState.

It is one of the built-in hooks in React. It helps us to declare state variables in Functional Components.

We pass the initial state to the useState hook, and in return, it gives us a variable with our state value and a function to update the value.

Now let’s compare both ways (JavaScript and React) with an example.

Suppose you’ve been given a bag filled with five apples and after some time, you are given seven more apples to put into your bag. Now how many apples will you have in your bag?

It's 5+7=12, right?

Let’s see how we can show this example in both JavaScript and React:

The JavaScript Way

I’ve created this repl.it HTML/CSS/JS code template to show this example live; you can access it here.

HTML

<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width">
   <title>replit</title>
   <link href="style.css" rel="stylesheet" type="text/css" />
 </head>
 <body>
   <div>
     <p>Apples</p>
     <p class="value">5</p>
     <button>Update values</button>
   </div>
   <script src="script.js"></script>
 </body>
</html>

JavaScript

const value = document.querySelector('.value')
const button = document.querySelector('button')

let realValue = parseInt(value.innerHTML)
let updateValue = () => {
 realValue+=7
 return value.innerHTML = realValue
}
button.addEventListener('click', updateValue)

Code Explanation

The React Way - useState()

Check out the repl of the same app created with React, here.

App.js

import React, {useState} from 'react';
import './App.css';

function App() {

 const [apples, setApples] = useState(5)
 const updateApples = () => {
   return setApples(apples+7)
 }
 return (
   <div>
     <p>Apples</p>
     <p>{apples}</p>
     <button onClick={updateApples}>Update Apples</button>
   </div>
 );
}

export default App;

Code Explanation

const [apples, setApples] = useState(5)
return setApples(apples+7)
return (
   <div>
     <p>Apples</p>
     <p>{apples}</p>
     <button onClick={updateApples}>Update Apples</button>
   </div>
 );
}

Counter App with useState()

Before starting, if you want to know how this app will work, you can check out this repl.

Code

import React, {useState} from 'react';
import './App.css';

function App() {

 const [addCount, setAddCount] = useState(0);
 const [subtractCount, setSubtractCount] = useState(0);

 return (
   <div>
     <p>Your value is {addCount}</p>
     <button onClick={() => setAddCount(addCount + 1)}>
       Add
     </button>

     <p>Your value is {subtractCount}</p>
     <button onClick={() => setSubtractCount(subtractCount - 1)}>
       Subtract
     </button>
   </div>
 );
}
export default App;

Explanation

Great, now both the Update Apple and Counter App are finished, and I hope you would have understood by now what useState hook is and how it works. If yes, show your love by sharing your thoughts and the article on social media.

Happy Coding ✌️


Also published here.