I have an input in a nested component. The state for that component is in its parent component. (The state can't be in the child component.) The problem is every input change, it loses focus, and the user has to click back on the input. I tried wrapping SearchComponent
in React.memo
, but it still didn't work. What am I doing wrong and how can I fix it?
Parent Component
const MainTable = props => {
const [myString, setMyString] = useState('');
const changeHandler = value => {
setMyString(value);
}
<MyInputComponent
changeHandler={changeHandler}
myString={myString}
/>
Nested Component
const SearchComponent = props => {
const [searchQuery, setSearchQuery] = useState('');
return (
<input
type='text'
value={props.myString}
onChange={e => {
props.changeHandler(e.target.value);
}}
/>
);
};