Abhilash Lohar
Abhilash Lohar

Reputation: 11

how to set value in TextInput using UseRef React hook

I am trying to set value in text input using the UseRef hook.

inputEl.current.value = "hello"

but it is not working.

Upvotes: 0

Views: 1373

Answers (2)

Shivam
Shivam

Reputation: 2359

You can use

let textInput = React.useRef(null);
const [text, setText] = React.useState('');

<TextInput
     ref={(input) => textInput = input}
     value={text}
     placeholder='Enter here'
     onChangeText={(value) => setText(value)}
/>

Upvotes: 2

Kavitha K T
Kavitha K T

Reputation: 119

const val = inputEl.current.value = "hello"
<input type =  "text" value = {val} ref = {inputEl}/>

provide obtained value using value attribute

Upvotes: 0

Related Questions