React.useState does not reload state from props
Solution 1:
The argument passed to useState is the initial state much like setting state in constructor for a class component and isn't used to update the state on re-render
If you want to update state on prop change, make use of useEffect
hook
function Avatar(props) {
const [user, setUser] = React.useState({...props.user});
React.useEffect(() => {
setUser(props.user);
}, [props.user])
return user.avatar ?
(<img src={user.avatar}/>)
: (<p>Loading...</p>);
}
Working demo
Solution 2:
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