How to clear the TextInput on React Native?

47 views Asked by At

it is the state: const[value, setValue]= useState();

it is the code:

    <View style={styles.messaginginputContainer}>
        <TextInput
        multiline
            style={styles.messaginginput}
            onChangeText={txt => setNote(txt)}
            value={value}
        />
        <Pressable
                onPress={()=>{
            clearValue('');
        }}
        >
            <View>
                <Text style={{ color: "#f2f0f1", fontSize: 20 }}>ok</Text>
            </View>
        </Pressable>
        
    </View>

i dont know how to do this functional

Ok, i can search a little bit more, but i want to enable the vote on my account

2

There are 2 answers

0
Hans Murangaza DRCongo On BEST ANSWER

This question has answer here: https://stackoverflow.com/a/45250198/13490165

create a button, then use

<TextInput ref={input => { textInput = input }} />

and clear like this:

textInput.clear()
0
Mehdi Faraji On

The easiest way to clear any text input is by clearing the state like this :

import { TouchableOpacity , TextInput } from 'react-native';

const [ inputValue , setInputValue ] = useState<string>("")

const clearInputValue = (): void => setInputValue("")

return (
    <>
       <TextInput value={inputValue} onChangeText={value => setInputValue(value)} />
       <TouchableOpacity onPress={clearInputValue}>Clear Input value</TouchableOpacity>
    </>
)