How To Bind Data In React JS

Websolutionstuff | Aug-19-2022 | Categories : React JS

In this article, we will see how to bind data in React JS. Also, we will see how to bind the variable value in the UI and the input fields. We can simply use the curly brace to bind the variable. Data Binding is the process of connecting the view element or user interface, with the data which populates it. if you want to display the updated variable value to UI is not easy. We need to use the useState Hook.

So, let's see data binding in React JS or two way data binding in React JS.

Binding Variable:

The binding variable is simple. we need to declare a variable in a single curly brace.

function App() {
    let name="Websolutionstuff"

    return ( 
    <div className = "App" >
        <p>Hello {name}</p>
     </div>
    );
}

export default App;

 

 

Next, create a method and in the function, change the name and create a button and bind a click event with the button using the newly created function.

function App() {
    let name="Websolutionstuff"
    const changeName =()=>{
        name="Websolutionstuff | Web developing Website"
        console.log(name)
    }

    return ( 
    <div className = "App" >
        <p>Hello {name}</p>
        
       <button onClick={changeName}>Change Name</button>
     </div>
    );
}

export default App;

 If you click the button, the variable value is changed. However, it does not change the display value. Why because the variable is not reactive to adopt the changes. To address this problem, React introduces the useState hook.

 

useState Hook:

The useState hook is nothing but a syntax with a predefined setter function. The setter function is a normal function that is used to set the value for the variable.

Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class.

const [var, setVar] = useState(value)

#Example

const [name, setName ] = useState('Websolutionstuff')

name: It is a variable name here.

setName: It is a setter function, which is used to set values.

Use camel case for setter function. for example, if your variable is age, then the setter function is setAge.

Now, create a variable with the useState hook and change the value using the setter function.

import {useState} from 'react';

function App() {

    const [name,setName]=useState("Websolutionstuff")
    
    const changeName =()=>{
        setName("Websolutionstuff | Web developing Website")
    }

    return ( 
    <div className = "App" >
        <p>Hello {name}</p>
       <button onClick={changeName}>Change Name</button>
     </div>
    );
}

export default App;

If you click the button, then the value will be changed. So if you want a variable to update its display value, then always use useState Hook.

 

 

Input Field Binding:

Now, we will see how to bind the variables to input fields using the useState hook.

import {useState} from 'react';
function App() {
  
    const [age,setAge]=useState(30)
    
    const changeAge =()=>{
        setAge(45)
    }

    return ( 
    <div className = "App" >
        <p>Your age is {age}</p>
        
       <button onClick={changeAge}>Change Age</button>
       <br/>
       <input value={age} onChange={(e)=>setAge(e.target.value)} />
     </div>
    );
}

export default App;

 


You might also like:

Recommended Post
Featured Post
Laravel tips: DB Models and Eloquent - Part 2
Laravel tips: DB Models and El...

Laravel, the PHP web application framework, is renowned for its elegant and efficient approach to handling databases thr...

Read More

Oct-13-2023

How To Copy Text To Clipboard In React JS
How To Copy Text To Clipboard...

In this article, we will see how to copy text to the clipboard in react js. you will learn how to copy text to...

Read More

Aug-31-2022

Laravel 9 Barcode Generator Example
Laravel 9 Barcode Generator Ex...

In this article, we will see laravel 9 barcode generator example. In this example we will use the milon/barcod...

Read More

Mar-26-2022

Laravel 9 Generate PDF From HTML Using TCPDF
Laravel 9 Generate PDF From HT...

In this article, we will see laravel 9 generate PDF from HTML using TCPDF. Here, we will learn how to integrate tcpdf in...

Read More

Jan-31-2023