It's a common React knowledge that having state initialized by props is bad if we don't make them in sync. This is considered fine:
import { useState, useEffect } from 'react';
export default function MyInput({ initialValue }) {
const [value, setValue] = useState(initialValue);
useEffect(
() => setValue(initialValue),
[initialValue]
);
return (
<>
<h1>The value is {value}</h1>
<input
type="text"
value={value}
onChange={event => setValue(event.target.value)}
/>
</>
);
}
But what if I actually don't want to update the value when initialValue
changes and want to remove the useEffect()
here? Is it strongly against React philosophy? It makes sense in my case, as I actually don't want to update this input value when something else changes the value passed as initialValue
. I don't want users to lose their input when that happens.
How bad is it?