Reputation: 145
const [MIV, setMIV] = useState(MIValues);
MIValues <- this data is received from props, If this data updated in other component and passed to here will the MIV has the updated value ?
Upvotes: 1
Views: 2491
Reputation:
Yes, component will reload automatically by react if the props(MiValues) is updated in parent component.And if you want to do something conditionally only when (MiValues) is updated, you can make use of useEffect hook.
Upvotes: 0
Reputation: 1
You should use useEffect
for it.
const [MIV, setMIV] = useState();
useEffect(() => {
setMIV(MIValues);
}, [MIValues])
Upvotes: 0
Reputation: 38094
useState
is the initial state. It is like constructor in class.useState
isn't used to update the state on re-render.
Does useEffect run after every render? Yes! By default, it runs both after the first render and after every update. (We will later talk about how to customize this.) Instead of thinking in terms of “mounting” and “updating”, you might find it easier to think that effects happen “after render”. React guarantees the DOM has been updated by the time it runs the effects.
Component will be updated if you use useEffect
:
const [MIV, setMIV] = useState(MIValues);
useEffect(() => {
setUser(props.user);
}, [MIValues])
Upvotes: 3