I'm trying to use refs created from an array but when I check in the Component tab of the React Developper Tools, ref passed to the input are always undefined. Something I missed here?
const MyComponent = () => {
const refs = useMemo(() => Array(2).fill(0).map(i => React.createRef()), []);
const TextInput = ({ name, value, inputRef }) => (
<input
name={name}
value={value}
onChange={updateField()}
ref={inputRef}
/>
)
return (
<TextInput
name="lastname"
value="Nom *"
inputRef={refs[0]}
/>
<TextInput
name="firstname"
label="Prénom *"
inputRef={refs[1]}
/>
)
}
Thanks!
iinrefs[i]?ianymore. I updated my sample code.useRefs?TextInputcomponents. But it can occur that the number of components vary and I have to generaterefs dynamically in that case.