TextInput tersuai React Native tidak bertindak balas kepada acara onChangeText
P粉743288436
P粉743288436 2023-09-21 22:59:27
0
2
619

Saya mahu mencipta dan menggunakan CustomTextInput dalam React Native. Saya telah menciptanya mengikut kod di bawah tetapi sifat onChangeText dalam CustomTextInput tidak berfungsi dengan betul.

Walaupun penyelidikan yang meluas, saya tidak dapat mengetahui punca masalah itu. Apa yang mungkin saya terlepas?

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;

Anda juga boleh menyemak di sini https://snack.expo.dev/@cemyeten/handling-text-input

P粉743288436
P粉743288436

membalas semua(2)
P粉938936304

Seperti yang saya lihat anda mencipta komponen di dalam komponen dan menggunakannya.

Walau bagaimanapun, memandangkan anda mencipta komponen berfungsi di dalam komponen, ia akan dicipta semula setiap kali kemas kini keadaan berlaku.

Pilihan yang lebih baik ialah mengalihkan CustomTextInput dari skrin atau mana-mana komponen yang mempunyai keadaan.

Contohnya:

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;
P粉754473468

Letakkan komponen anda dalam fungsi App函数之外,或者更好的方法是为其创建一个单独的文件,因为如果您将其放在内部,当您编写文本useState hook重新渲染App untuk mencerminkannya pada UI, ini akan membuatkan komponen anda hilang fokus.

Kod tetap:

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;
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!