Arsiki
Arsiki

Reputation: 378

change State after onPressIn and onPressOut TextInput react-native

i want to change useState boolean value after focusing on textinput and also after touching out of it. but it doesn't work. what is my mistake?

const {chatmode,setChatmode} = useState(false);

    return(
            <View style={[styles.chat,{zIndex:chatmode? 1 :-1}]}>
                <TextInput style={styles.chatinput} onPressOut={()=> setChatmode(false)} onPressIn={setChatmode(true)}/>
            </View>
    )

result error is :

TypeError: setChatmode is not a function. (In 'setChatmode(true)', 'setChatmode' is undefined)

Upvotes: 0

Views: 603

Answers (1)

David Scholz
David Scholz

Reputation: 9806

You need to use [] brackets not curly brackets as follows

const [chatmode, setChatmode] = useState(false)

Edit: To answer your question concerning onPressOut. You could wrap your TextInput into Pressable.

<View>
       <Pressable onPressIn={() => console.log("in")} onPressOut={() => console.log("out")}>
         <TextInput></TextInput>
       </Pressable>
</View>

Upvotes: 1

Related Questions