How to change props to state in React Hooks?

The problem in your code is with {}.
UseEffect hook expects and array, not an object. Use [initialDesc] instead.

This is how you reset component state when props change

const GenerateDescHook = ({ description: initialDesc }) => {
  const [description, setDescription] = useState(null)

  useEffect(() => {
    setDescription(initialDesc)
  }, [initialDesc]);
}

This is how you initialize component state to prop value on first render only

const GenerateDescHook = ({ description: initialDesc }) => {
  const [description, setDescription] = useState(initialDesc);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)