How to sync props to state using React hook : setState()


February 2019


4 time


I am trying to set the state using React hook setState() using the props the component receive. I've tried using the below code:

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

const Persons = (props) =>  {

    // console.log(;

   const [nameState , setNameState] = useState(props)


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


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.

0 answers