Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 41 characters in body
Source Link
gmoniava
  • 28.9k
  • 9
  • 61
  • 103

If I correctly understood you basically want to copy user.name prop to some variable such that future updates to props.user.name are ignored by that variable. In such case you must copy that props to a state variable:

 // inside Edit   
 const [previousName, setPreviousName] = useState(props.user.name); // You could also use useRef instead

This way previousName will get initialized with props.user.name when that component mounts for the first time. Further updates to props.user.name will be ignored by this state variable.

Only if you unmount the Edit component and mount again, then it will copy the props.user.name again into previousName.

PS Alternatively you can use useRef instead of useState in my example

If I correctly understood you basically want to copy user.name prop to some variable such that future updates to props.user.name are ignored by that variable. In such case you must copy that props to a state variable:

 // inside Edit   
 const [previousName, setPreviousName] = useState(props.user.name); // You could also use useRef instead

This way previousName will get initialized with props.user.name when that component mounts for the first time. Further updates to props.user.name will be ignored by this state variable.

Only if you unmount the Edit component and mount again, then it will copy the props.user.name again into previousName.

If I correctly understood you basically want to copy user.name prop to some variable such that future updates to props.user.name are ignored by that variable. In such case you must copy that props to a state variable:

 // inside Edit   
 const [previousName, setPreviousName] = useState(props.user.name); 

This way previousName will get initialized with props.user.name when that component mounts for the first time. Further updates to props.user.name will be ignored by this state variable.

Only if you unmount the Edit component and mount again, then it will copy the props.user.name again into previousName.

PS Alternatively you can use useRef instead of useState in my example

added 36 characters in body
Source Link
gmoniava
  • 28.9k
  • 9
  • 61
  • 103

If I correctly understood you basically want to copy user.name prop to some variable such that future updates to props.user.name are ignored by that variable. In such case you must copy that props to a state variable:

 // inside Edit   
 const [previousName, setPreviousName] = useState(props.user.name); // You could also use useRef instead

This way previousName will get initialized with props.user.name when that component mounts for the first time. Further updates to props.user.name will be ignored by this state variable.

Only if you unmount the Edit component and mount again, then it will copy the props.user.name again into previousName.

If I correctly understood you basically want to copy user.name prop to some variable such that future updates to props.user.name are ignored by that variable. In such case you must copy that props to a state variable:

 // inside Edit   
 const [previousName, setPreviousName] = useState(props.user.name); 

This way previousName will get initialized with props.user.name when that component mounts for the first time. Further updates to props.user.name will be ignored by this state variable.

Only if you unmount the Edit component and mount again, then it will copy the props.user.name again into previousName.

If I correctly understood you basically want to copy user.name prop to some variable such that future updates to props.user.name are ignored by that variable. In such case you must copy that props to a state variable:

 // inside Edit   
 const [previousName, setPreviousName] = useState(props.user.name); // You could also use useRef instead

This way previousName will get initialized with props.user.name when that component mounts for the first time. Further updates to props.user.name will be ignored by this state variable.

Only if you unmount the Edit component and mount again, then it will copy the props.user.name again into previousName.

added 18 characters in body
Source Link
gmoniava
  • 28.9k
  • 9
  • 61
  • 103

If I correctly understood you basically want to copy user.name prop to some variable such that future updates to props.user.name are ignored by that variable. In such case you must copy that props to a state variable:

 // inside Edit   
 const [previousName, setPreviousName] = useState(props.user.name); 

This way previousName will get initialized with props.user.name when that component mounts for the first time. Further updates to props.user.name will be ignored by this state variable.

Only if you unmount the Edit component and mount again, then it will copy the props.user.name again into previousName.

If I correctly understood you basically want to copy user.name prop to some variable such that future updates to user.name are ignored by that variable. In such case you must copy that props to a state variable:

const [previousName, setPreviousName] = useState(props.user.name); 

This way previousName will get initialized with user.name when that component mounts for the first time. Further updates to user.name will be ignored by this state variable.

If I correctly understood you basically want to copy user.name prop to some variable such that future updates to props.user.name are ignored by that variable. In such case you must copy that props to a state variable:

 // inside Edit   
 const [previousName, setPreviousName] = useState(props.user.name); 

This way previousName will get initialized with props.user.name when that component mounts for the first time. Further updates to props.user.name will be ignored by this state variable.

Only if you unmount the Edit component and mount again, then it will copy the props.user.name again into previousName.

Source Link
gmoniava
  • 28.9k
  • 9
  • 61
  • 103
Loading