React Native 无法写入 TextInput

问题描述 投票:0回答:3

我正在创建一个应用程序,用户可以在其中输入一些文本。我想要用户在终端中输入的文本作为日志。

我的问题是,当我启动应用程序时,我无法在文本字段中写入任何内容。

App.js:

    import { useState } from 'react';
    import { Button, StyleSheet, Text, View, TextInput } from 'react-native';
    
    export default function App() {
    
      const [enteredKeyword, setEnteredKeyword] = useState('') /* Bind TextInput to the variables */
    
      
      const keywordInputHandler = (enteredText) => {
        setEnteredKeyword(enteredText)
      }
      const addKeywordHandler = () => {
        console.log(enteredKeyword)
      }
    
      return (
        <View style={styles.container}>
    
          { /* Header */ }
          <View style={styles.header}>
            <TextInput placeholder="Enter text" style={styles.textInput} onChangedText={keywordInputHandler} value={enteredKeyword} />
            <Button title="Add" onPress={addKeywordHandler} />
          </View>
    
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
      },
    
    
      header: {
        flexDirection: 'row',
        justifyContent: 'space-between',
        alignItems: 'center'
      },
      textInput: {
        width: '80%',
        borderColor: 'black',
        borderWidth: 1,
        padding: 10
      }
    });
react-native textinput
3个回答
2
投票

请将

onChangedText
更改为
onChangeText

另请查看 TextInput 的指南:https://reactnative.dev/docs/textinput


0
投票
onChangeText={(text)=> keywordInputHandler(text)}

0
投票

你写了

onChangedText
,而它应该是
onChangeText
,就像这样:

<TextInput placeholder="Enter text" style={styles.textInput} onChangeText={keywordInputHandler} value={enteredKeyword} />
© www.soinside.com 2019 - 2024. All rights reserved.