Niyasin
Niyasin

Reputation: 133

How can I use ref to get the input value of TextInput in ReactNative?

I want to console log the text in the TextInput once the Button is pressed. I want to use useRef() to reference the TextInput.I tried this in react and works fine.But it is not working in react native

import { useRef } from "react";
import { Button, TextInput, View } from "react-native";

export default function App() {
    const inputRef=useRef();
    const send=()=>{
        console.log(inputRef.current.value);
    }
    return(
        <View>
            <TextInput ref={inputRef}/>
            <Button
                title="send"
                onPress={send}
                ></Button>
        </View>

    )
}

Upvotes: 1

Views: 761

Answers (1)

Ploppy
Ploppy

Reputation: 15363

You can use a state.

const [value, setValue] = useState("");

function send(){
  console.log(value);
}

return (
  <TextInput value={value} onChange={setValue}/>
);

Upvotes: 1

Related Questions