React Native custom TextInput does not respond to onChangeText event
P粉743288436
P粉743288436 2023-09-21 22:59:27
0
2
565

I want to create and use a CustomTextInput in React Native. I have created it as per the code below but the onChangeText property in CustomTextInput is not working properly.

Despite extensive research, I can't figure out the cause of the problem. What might I have missed?

import React, { useState } from 'react'; import { View, StyleSheet } from 'react-native'; import { TextInput } from 'react-native'; const App = () => { const [inputValue, setInputValue] = useState(''); const CustomTextInput = ({ value, onChangeText, placeholder, style }) => { return (  ); }; const handleTextChange = (text) => { setInputValue(text); }; return (     ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0', }, textInput: { width: '80%', height: 40, borderWidth: 1, borderColor: 'gray', padding: 10, }, }); export default App;

You can also check here https://snack.expo.dev/@cemyeten/handling-text-input

P粉743288436
P粉743288436

reply all (2)
P粉938936304

As I can see, you create a component inside the component and use it.

However, since you created a functional component inside the component, it will be recreated every time a state update occurs.

A better option is to move the CustomTextInput off the screen or any component with state.

For example:

import React, { useState } from 'react'; import { View, StyleSheet } from 'react-native'; import { TextInput } from 'react-native'; const CustomTextInput = ({ value, onChangeText, placeholder, style }) => { return (  ); }; const App = () => { const [inputValue, setInputValue] = useState(''); const handleTextChange = (text) => { setInputValue(text); }; return (     ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0', }, textInput: { width: '80%', height: 40, borderWidth: 1, borderColor: 'gray', padding: 10, }, }); export default App;
    P粉754473468

    Put your component outside theAppfunction or better yet create a separate file for it because if you put it inside, when you write the text useState hook re Render theAppfunction to reflect it on the UI, which will cause your component to lose focus.

    Fixed code:

    import React, { useState } from 'react'; import { View, StyleSheet } from 'react-native'; import { TextInput } from 'react-native'; const CustomTextInput = ({ value, onChangeText, placeholder, style,...other }) => { return (  ); }; const App = () => { const [inputValue, setInputValue] = useState(''); const handleTextChange = (text) => { setInputValue(text); }; return (     ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0', }, textInput: { width: '80%', height: 40, borderWidth: 1, borderColor: 'gray', padding: 10, }, }); export default App;
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!