As a beginner on react native, I cant find solution anywhere for this. For example given on React Native Docs
I want to access .focus, .blur, clear() methods.
Thanks in advance.
As a beginner on react native, I cant find solution anywhere for this. For example given on React Native Docs
I want to access .focus, .blur, clear() methods.
Thanks in advance.
UPDATE:
You should assign a reference to the input text field using something like this:
const textInputRef = useRef(null);
...
<TextInput
ref={(input) => { textInputRef.current = input; }}
placeholder="Text Input"
/>
And use this ref to trigger the .focus etc events.
for example
<Button
label="Focus on Input"
onClick={() => { this.textInputRef.current.focus(); }}
/>
For more details you can refer to following answer here
OLD ANSWER (as per my understanding of the question):
You can easily use these events using similar props (based on the component you are using)
For example, for TextInput
import React from "react";
import { SafeAreaView, StyleSheet, TextInput } from "react-native";
const UselessTextInput = () => {
const [text, onChangeText] = React.useState("Useless Text");
const [focused, setFocused] = React.useState(null);
return (
<SafeAreaView>
<TextInput
style={styles.input}
onChangeText={onChangeText}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
value={text}
/>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
});
export default UselessTextInput;
To use these imperative methods, store the component instance in a React ref.
const UselessTextInput = () => {
const textInputRef = React.useRef(null); // new
const [text, onChangeText] = React.useState("Useless Text");
const [focused, setFocused] = React.useState(null);
return (
<SafeAreaView>
<TextInput
ref={textInputRef}
style={styles.input}
onChangeText={onChangeText}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
value={text}
/>
{/* New */}
<TouchableOpacity onPress={() => textInputRef.current?.focus()}>
<Text>Focus text input</Text>
</TouchableOpacity>
</SafeAreaView>
);
};
You can read more about this in the "Refs and the DOM" section of the React docs.
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
} from "react-native";
import React, { useRef, useState } from "react";
export default function App(props) {
const textInputRef = useRef(null);
const [text, setText] = useState("Useless Text");
const [focused, setFocused] = useState(null);
return (
<View style={styles.container}>
<TextInput
ref={textInputRef}
style={styles.input}
onChangeText={setText}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
value={text}
/>
<TouchableOpacity
style={styles.button}
onPress={() => textInputRef.current.clear()}
>
<Text>Clear text input</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
button: {
backgroundColor: "dodgerblue",
margin: 50,
padding: 20,
borderRadius: 20,
},
input: {
backgroundColor: "green",
height: 100,
width: 300,
borderRadius: 25,
textAlign: "center",
},
});
I have used this finally just for reference of other users.