Functional components where we use useState
to set initial values to our variable, if we pass initial value through props, it will always set same initial value until you don't make use of useEffect
hook,
for example your case this will do your job
React.useEffect(() => {
setUser(props.user);
}, [props.user])
The function passed to useEffect will run after the render is committed to the screen.
By default, effects run after every completed render, but you can choose to fire them only when certain values have changed.
React.useEffect(FunctionYouWantToRunAfterEveryRender)
if you pass only one argument to useEffect it will run this method after every render
you can decide when to fire this FunctionYouWantToRunAfterEveryRender
by passing second argument to useEffect
React.useEffect(FunctionYouWantToRunAfterEveryRender, [props.user])
as you notice i am passing [props.user] now useEffect
will only fire this FunctionYouWantToRunAfterEveryRender
function when props.user
is changed
i hope this helps your understanding
let me know if any improvements are required thanks
props.user
. – Hubbub