Home  >  Q&A  >  body text

React Native custom TextInput does not respond to onChangeText event

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 (
    <TextInput
      value={value}
      onChangeText={onChangeText}
      placeholder={placeholder}
      style={style}
    />
  );
};


  const handleTextChange = (text) => {
    setInputValue(text);
  };

  return (
    <View style={styles.container}>
      <CustomTextInput
        value={inputValue}
        onChangeText={handleTextChange}
        placeholder="自定义,不工作..."
        style={styles.textInput}
      />

     <TextInput
      value={inputValue}
      onChangeText={handleTextChange}
      placeholder={"工作中.."}
      style={styles.textInput}
    />

    </View>

    
  );
};

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粉743288436P粉743288436394 days ago642

reply all(2)I'll reply

  • P粉938936304

    P粉9389363042023-09-22 17:15:23

    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 (
        <TextInput
          key='a'
          value={value}
          onChangeText={onChangeText}
          placeholder={placeholder}
          style={style}
        />
      );
    };
    
    const App = () => {
      const [inputValue, setInputValue] = useState('');
    
      const handleTextChange = (text) => {
        setInputValue(text);
      };
    
      return (
        <View style={styles.container}>
          <CustomTextInput
            value={inputValue}
            onChangeText={handleTextChange}
            placeholder="自定义,不起作用..."
            style={styles.textInput}
          />
         <TextInput
          value={inputValue}
          onChangeText={handleTextChange}
          placeholder={"起作用.."}
          style={styles.textInput}
        />
        </View>
      );
    };
    
    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;

    reply
    0
  • P粉754473468

    P粉7544734682023-09-22 14:40:33

    Put your component outside the App function or better yet create a separate file for it because if you put it inside, when you write the text useState hook re Render the App function 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 (
        <TextInput
          value={value}
          onChangeText={onChangeText}
          placeholder={placeholder}
          style={style}
          {...other}
        />
      );
    };
    
    const App = () => {
      const [inputValue, setInputValue] = useState('');
    
      const handleTextChange = (text) => {
        setInputValue(text);
      };
    
      return (
        <View style={styles.container}>
          <CustomTextInput
            value={inputValue}
            onChangeText={handleTextChange}
            placeholder="自定义,不起作用..."
            style={styles.textInput}
          />
    
         <TextInput
          value={inputValue}
          onChangeText={handleTextChange}
          placeholder={"起作用了.."}
          style={styles.textInput}
        />
    
        </View>
    
        
      );
    };
    
    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;
    

    reply
    0
  • Cancelreply