搜尋

首頁  >  問答  >  主體

React Native自訂TextInput不回應onChangeText事件

我想在React Native中建立和使用一個CustomTextInput。我已經按照下面的程式碼創建了它,但是CustomTextInput中的onChangeText屬性沒有正常工作。

儘管進行了廣泛的研究,但我無法找出問題的原因。我可能漏掉了什麼?

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;

你也可以在這裡檢查 https://snack.expo.dev/@cemyeten/handling-text-input

P粉743288436P粉743288436475 天前708

全部回覆(2)我來回復

  • P粉938936304

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

    如我所見,您在元件內部建立了一個元件並使用它。

    但是,由於您在元件內部建立了一個功能元件,每次發生狀態更新時它都會重新建立。

    更好的選擇是將CustomTextInput移出螢幕或任何具有狀態的元件。

    例如:

    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;

    回覆
    0
  • P粉754473468

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

    將您的元件放在App函數之外,或者更好的方法是為其創建一個單獨的文件,因為如果您將其放在內部,當您編寫文字useState hook重新渲染App函數以在UI上反映它,這將使您的元件失去焦點。

    修復的程式碼:

    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;
    

    回覆
    0
  • 取消回覆