I have a re-usable component <Layout />
that allows me to pass in custom components into a content
and sidebar
property.
In one instance of this, I'm putting an input field into the content which is a controlled component and uses useState()
to handle the data.
However, I'm finding that my keyboard is losing focus whenever I type into the input. It appears the entire form is re-rendering and I can't stop it from doing so.
It was fine when my code was all inline, but since refactoring it to use the <Layout />
component, it is having some strange effects.
- I do not wish to use any extras libraries other than core React 16.x
- I have tried using
useCallback()
, keys, names, ids, refs - all to no avail - I've moved the callback functions out of scope and passed values through as per this answer but the problem persists
import React, { useState } from 'react';
function Layout({ content: Content, sidebar: Sidebar }) {
return (
<>
<div>
<Content />
</div>
<div>
<Sidebar />
</div>
</>
);
}
function Page() {
const [value, setValue] = useState('');
return (
<>
<Layout
content={() => (
<input
value={value}
onChange={(event) => setValue(event.target.value)}
/>
)}
sidebar={() => (
<p>Lorem ipsum...</p>
)}
/>
</>
);
}
export default Page;