React native 在React Native中,文本输入字段不允许我编辑或键入新文本

React native 在React Native中,文本输入字段不允许我编辑或键入新文本,react-native,textinput,React Native,Textinput,我注意到当我尝试在文本输入字段中键入任何内容时,它会自动删除它。我已经把问题缩小到值字段,注释它可以让我输入文本,但我不确定问题出在哪里 import React, { useState } from 'react'; import { StyleSheet, Text, View, TextInput, Button } from 'react-native'; export default function App() { const [enteredGoal, setEnte

我注意到当我尝试在文本输入字段中键入任何内容时,它会自动删除它。我已经把问题缩小到值字段,注释它可以让我输入文本,但我不确定问题出在哪里

    import React, { useState } from 'react';
import { StyleSheet, Text, View, TextInput, Button } from 'react-native';

export default function App() {
  const [enteredGoal, setEnteredGoal] = useState('');

  const goalInputHandler = (enteredText) => {
    setEnteredGoal(enteredGoal);
  };

  const addGoalHandler = () => {
    console.log(enteredGoal);
  };

  return (
    <View style={styles.screen}>
      <View style={styles.inputContainer}>
        <TextInput 
          placeholder="Course Goal"
          style={styles.input}
          onChangeText={goalInputHandler}
          value={enteredGoal}
        />
        <Button title='Add' onPress={addGoalHandler} />
      </View>
      <View />
    </View>
  );
}
import React,{useState}来自“React”;
从“react native”导入{样式表、文本、视图、文本输入、按钮};
导出默认函数App(){
const[enteredGoal,setEnteredGoal]=useState(“”);
常量goalInputHandler=(输入文本)=>{
setEnteredGoal(enteredGoal);
};
const addGoalHandler=()=>{
控制台日志(enteredGoal);
};
返回(
);
}
两个原因:

  • goalInputHandler()中的变量不匹配。传入
    enteredText
    ,但尝试使用
    enteredtoal
  • goalInputHandler()
    不返回任何内容。您需要使用括号而不是大括号,或者在
    setEnteredGoal()
    之前添加return语句
  • 正确代码:

    import React,{useState}来自“React”;
    从“react native”导入{样式表、文本、视图、文本输入、按钮};
    导出默认函数App(){
    const[enteredGoal,setEnteredGoal]=useState(“”);
    常量goalInputHandler=(输入文本)=>(
    setEnteredGoal(enteredText);
    );
    const addGoalHandler=()=>(
    控制台日志(enteredGoal);
    );
    返回(
    );
    }
    
    两个原因:

  • goalInputHandler()中的变量不匹配。传入
    enteredText
    ,但尝试使用
    enteredtoal
  • goalInputHandler()
    不返回任何内容。您需要使用括号而不是大括号,或者在
    setEnteredGoal()
    之前添加return语句
  • 正确代码:

    import React,{useState}来自“React”;
    从“react native”导入{样式表、文本、视图、文本输入、按钮};
    导出默认函数App(){
    const[enteredGoal,setEnteredGoal]=useState(“”);
    常量goalInputHandler=(输入文本)=>(
    setEnteredGoal(enteredText);
    );
    const addGoalHandler=()=>(
    控制台日志(enteredGoal);
    );
    返回(
    );
    }