Below is my parent component with multiple inputs from a loop. How can I choose one input
to focus? Do I have to create a dynamic ref
in this case?
// General Focus Hook
const useFocus = (initialFocus = false, id = "") => {
const [focus, setFocus] = useState(initialFocus)
return ([
(newVal=true) => setFocus(newVal), {
autoFocus: focus,
key: `${id}${focus}`,
onFocus: () => setFocus(true),
onBlur: () => setFocus(false),
},
])
}
const data: [{
name: "abc"
},{
name: "def"
}]
const TestRef = () => {
const focusHelper = data.map( (_,i) => {
const [setFocus, focusProps]= useFocus(false, i)
return {setFocus, focusProps}
})
return (
{data.map( (o,i) => (
;
))}
);
}
You can find more info here: Set focus on input after render