How to sync props to state using React hooks setState

0 votes

I am trying to set the state using React hook setState() using the props the component receive.

 I have tried using the below code:

import React,{useState , useEffect} from 'react';

const Persons = (props) =>  {

    // console.log(props.name);

   const [nameState , setNameState] = useState(props)

   console.log(nameState.name);
   console.log(props.name);

   return (
            <div>
                <p>My name is {props.name} and my age is {props.age}</p>
                <p>My profession is {props.profession}</p>
            </div>
        )

}

export default Persons;

The issue is the state is being set upon component being loaded. But when it receive new props, the state is not getting updated.

 How to update the state in this case?

 Thanks in advance

Jun 1, 2020 in Laravel by kartik
• 37,520 points
3,544 views

1 answer to this question.

0 votes

Hello @kartik,

useState hooks function argument is being used only once and not everytime the prop changes. You must make use of useEffect hooks to implement what you would call the componentWillReceiveProps/getDerivedStateFromProps functionality.

Code:

import React,{useState , useEffect} from 'react';

const Persons = (props) =>  {
   const [nameState , setNameState] = useState(props)

   useEffect(() => {
       setNameState(props);
   }, [props])

   return (
            <div>
                <p>My name is {props.name} and my age is {props.age}</p>
                <p>My profession is {props.profession}</p>
            </div>
        )

}

export default Persons;

Hope this work!!

If you need to know more about React, Its recommended to join React JS Course today.

Thank You!!

answered Jun 1, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
0 answers

how to create exception in react router using Route::get('/{path?}', 'IndexController@index')->name('index') ->where('path', '.*');

I am using react routers within Laravel ...READ MORE

Nov 15, 2020 in Laravel by datso
• 120 points

recategorized Nov 16, 2020 by Niroj 769 views
0 votes
1 answer

How to set a new value for data-url attribute using jquery?

Hii, In your line: $(this).attr('data-url',value.url); are you sure this refers to the ...READ MORE

answered Apr 14, 2020 in Laravel by Niroj
• 82,840 points
7,035 views
0 votes
1 answer

How to Install Laravel without using Composer?

Hello @kartik, If you really wanted to, you ...READ MORE

answered Aug 10, 2020 in Laravel by Niroj
• 82,840 points
4,192 views
0 votes
1 answer

How to exclude certains columns while using eloquent?

Hello @kartik, Using hidden array in model is good, but ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,840 points
15,526 views
0 votes
1 answer

How can I initialize state with props in React using hooks?

You can initialize state with props using ...READ MORE

answered Feb 12 in Node-js by Navya
114 views
0 votes
1 answer
0 votes
1 answer

How can props be passed using Link in React Router?

In React Router, you can pass data ...READ MORE

answered Feb 21 in Node-js by kavya
73 views
0 votes
1 answer

How can updated props be received in React JS?

Components receive data through props, which are ...READ MORE

answered Feb 21 in Node-js by kavya
65 views
0 votes
1 answer

How to make a new page with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
13,410 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,499 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP