Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onBlur don't work in text input react native

Im trying to detect when user click outside the input, but feel like it only work with onFocus but it onBlur don't do anything, here is the simple code

/* eslint-disable react-native/no-inline-styles */
import React, {useState} from 'react';
import {
  TouchableWithoutFeedback,
  TextInput,
  View,
  SafeAreaView,
} from 'react-native';

const TextInputTest = () => {
  const [isBorder, setBorder] = useState(false);
  return (
    <SafeAreaView
      style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <View style={{borderWidth: isBorder ? 1 : 0}}>
        <TextInput
          placeholder="sometext"
          onFocus={() => setBorder(!isBorder)}
          onBlur={() => setBorder(!isBorder)}
        />
      </View>
    </SafeAreaView>
  );
};
export default TextInputTest;

I can only see the border turn black and it never blank again, what happen, please help, thank you a lots

enter image description here

like image 610
normalDev Avatar asked Oct 23 '25 09:10

normalDev


1 Answers

onBlur event will be triggered when you focus out your TextInput. In this case, you have only one TextInput and you cant focus out to somewhere else as an action. Try to have 2 TextInput instead or define some actions such as an button then when you click on another TextInput or Button, onBlur will be called.

like image 196
An Nguyen Avatar answered Oct 24 '25 22:10

An Nguyen