Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to access the text inside a TextInput without having the value prop defined?

$
0
0

I want to create a form without using useState to control the inputs because I only need the inputs values when the user presses the button, so I want to access/know the input value at that time only. I also think that updating the screen after each text input is not a good practice.

Let's assume that we have the following structure:

const Home = () => {  // const [inputValue, setInputValue] = useState(""); => I want to avoid that  const inputRef = useRef<TextInput>(null);  return (<View><TextInput        style={styles.input}        ref={inputRef}        // value={inputValue} => I want to avoid that        // onChangeText={(text) => setInputValue(text)} => I want to avoid that      /></View>  );};

We all know that when we have one TextInput on screen, we can type in it, without specifying the value prop and onChangeText, that we would normally use useState for that.

What I want to do is simply access the text that was input, and I would use a ref for that using useRef hook, but the problem is that using useRef I have access to TextInput props but the value is undefined because I am not specifying that. Am I missing something or there is no way to do that without using a useState for defining a value and onChangeText?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>