Javascript “打印出”;“未定义”;关于react native中的onPress

Javascript “打印出”;“未定义”;关于react native中的onPress,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我有了一个新的React Native,并使用youtube从教程中学习 import React, { useState } from 'react'; import { StyleSheet, Text, View, TextInput, Button } from 'react-native'; export default function App() { const [enteredGoal, setEnteredGoal] = useState(''); const go

我有了一个新的
React Native
,并使用youtube从教程中学习

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(enteredText);
  }

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

  return (
    <View style={styles.screen}>
      <View style={styles.inputContainer}>
        <TextInput placeholder="Course Goals" style={styles.input}
          onChangeText={()=>goalInputHandler()} />
        <Button title="Add" onPress={() => addGoalHandler()}/>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: {
    padding: 50
  },
  inputContainer: {
    flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center'
  },
  input: {
    width: '80%', borderColor: 'black', borderWidth: 1, padding: 10
  }
});
import React,{useState}来自“React”;
从“react native”导入{样式表、文本、视图、文本输入、按钮};
导出默认函数App(){
const[enteredGoal,setEnteredGoal]=useState(“”);
常量goalInputHandler=(输入文本)=>{
setEnteredGoal(enteredText);
}
const addGoalHandler=()=>{
控制台日志(enteredGoal);
};
返回(
goalInputHandler()}/>
addGoalHandler()}/>
);
}
const styles=StyleSheet.create({
屏幕:{
填充:50
},
输入容器:{
flexDirection:“行”,justifyContent:“间距”,alignItems:“中心”
},
输入:{
宽度:“80%”,边框颜色:“黑色”,边框宽度:1,填充:10
}
});
onPress
我得到了
未定义的值。我已经创建了这个项目使用世博会


我认为您正在丢失
onChangeText
中的字符串,因为您调用
goalInputHandler
时没有任何参数,因此它总是
未定义的。你能试试这个吗


我认为您正在丢失
onChangeText
中的字符串,因为您调用
goalInputHandler
时没有任何参数,因此它总是
未定义的。你能试试这个吗



棒极了,速度很快+1非常感谢@HermitCrab:)棒极了,速度很快+1非常感谢@HermitCrab:)欢迎来到Stack Overflow。为了激励你,我对你的帖子投上一票。欢迎来到Stack Overflow。我对你的职位投赞成票,以激励你。