React Native无法处理console.log中的输出

5q4ezhmt  于 2023-10-22  发布在  React
关注(0)|答案(1)|浏览(204)
const [task, setTask] = useState()
const [taskItems, setTaskItems] = useState([])

const handleAddTask = () => {
  setTaskItems([...taskItems, task])
  setTask(null)
  console.log(task) <--
}

  return (
<View style={styles.container}>
    <View style={styles.tasksWrapper}>
      <Text style={styles.sectionTitle}>Today's tasks</Text>
      <View style={styles.items}>
          <Task text={'Task 1'} />
          <Task text={'Task 2'} />
          <Task text={'Task 3'} />
          <Task text={'Task 4'} />
      </View>
    </View> 
    <KeyboardAvoidingView behavior={Platform.OS === "ios" ? "padding" : "height"} style={styles.writeTaskWrapper}>
        <TextInput style={styles.input} placeholder={"Write a task"} onChange={text => setTask(text)}/>
        <TouchableOpacity onPress={() => handleAddTask()}>
            <View style={styles.addWrapper}>
                <Text style={styles.addText}>+</Text>
            </View>
        </TouchableOpacity>
    </KeyboardAvoidingView>
</View>
);

我使用expo创建了一个react native应用程序,我想使用console.log(xxx)在控制台上显示结果。有什么好的建议在哪里打开控制台,以查看日志结果?

ego6inou

ego6inou1#

首先,尝试更改这些行:

<TextInput style={styles.input} placeholder={"Write a task"} onChange={text => setTask(text)}/>
<TouchableOpacity onPress={() => handleAddTask()}>

收件人:

<TextInput style={styles.input} placeholder={"Write a task"} onChangeText={setTask}/>
<TouchableOpacity onPress={handleAddTask}>

相关问题