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,508 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 740 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
6,986 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,146 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,359 views
0 votes
0 answers

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

With the help of code can you ...READ MORE

Feb 12 in Node-js by Nidhi
• 8,520 points
36 views
0 votes
0 answers

What is the method to override props in a React Native library?

Can you tell me What is the ...READ MORE

Feb 12 in Node-js by Nidhi
• 8,520 points
39 views
0 votes
0 answers

How can props be passed using Link in React Router?

Can you tell me How can props ...READ MORE

Feb 12 in Node-js by Nidhi
• 8,520 points
26 views
0 votes
0 answers

How can updated props be received in React JS?

With the help of proper examples can ...READ MORE

Feb 12 in Node-js by Nidhi
• 8,520 points
25 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,294 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,363 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