when pressing an Icon, I want the TextInput to lose focus. I found this: Unfocus a TextInput in React Native and tried everything, but its just not working. Like this I get the error:
Undefined is not an object (evaluating '_this.myInput.blur')
My Code:
<TextInputCustom
ref={(ref) => { this.myInput= ref }}
iconType={
Platform.OS === "android" ?
isSearchbarFocused? 'chevron-left' :
'search'
:'search'}
iconPress= {() => { Platform.OS === "android" && isSearchbarFocused ?(setSearchbarFocused(false), this.myInput.blur()):(console.log("Hi")}}
/>
My TextInputCustom is looking like this:
const TextInputCustom = ({
iconType,
placeholder,
onChange,
onFocus,
textContentType,
autoCapitalize,
autoCompleteType,
iconPress,
ref
}) => {
return (
<View style={styles.inputView}>
<Icon name={iconType} onPress={iconPress} size={20}
/>
<TextInput
style={{
flex: 1,
paddingHorizontal: 12,
}}
placeholder={placeholder}
textContentType={textContentType}
autoCapitalize={autoCapitalize}
autoCompleteType={autoCompleteType}
autoCorrect={false}
onChangeText={(e) => onChange(e)}
onFocus={onFocus}
/>
</View>
);
};
Or is the mistake, that Im using a custom element, so I cant use .blur like this? What am I doing wrong, and how can I do this?