i cant seem to get my head around this. I want to to use a button to use .focus() on my textinput in react native. How do i pass the ref to my function and call that method?
const [{ingredients}, dispatch] = React.useContext(AddRecipeContext);
const [edit, setEdit] = React.useState('');
const startEdit = (key: string, ref: any) => {
ref.current.focus();
setEdit((prev) => (prev === key? '':key))
}
const _renderItem = ({ item }: {item: IIngredient}) => {
var textInputRef: any = React.createRef();
return (
<View key={item.key}>
<TextInput
ref={textInputRef}
onChangeText={label => setIngredientLabel(item.key, label) }
value={item.label.toString()}/>
<Pressable onPress={() => startEdit(item.key, textInputRef)}/>
</View>
);
};
const RenderIngredients = ingredients.map((item: IIngredient) => _renderItem({item}));
return (
<View>
<ScrollView>
{RenderIngredients}
</ScrollView>
</View>
);
What is the correct way to handle refs?
Thank you in advance.
EDIT: ref.focus(); to ref.current.focus();