javascript - react native : functions are not valid as a react child . 如果您返回组件而不是 <component/>,则可能会发生这种情况

标签 javascript reactjs react-native

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

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

  const [courseGoals, setCourseGoals] = useState([]);

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

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

    setCourseGoals(currentGoals => [...currentGoals, 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>{(courseGoals.map = goal => <Text key={goal}>{goal}</Text>)}</View>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: {
    padding: 50
  },
  inputContainer: {
    flexDirection: "row",
    justifyContent: "space-between",
    alignItems: "center"
  },

  input: {
    width: "70%",
    borderBottomColor: "black",
    borderWidth: 1,
    padding: 10
  }
});

拜托,我已经与这个错误作斗争一个多星期了。任何帮助将不胜感激。我实际上正在遵循使用 expo 在 youtube 上构建 React Native 应用程序的教程。

最佳答案

问题是您的 map 调用,将其更改为:

<View>{courseGoals.map(goal => <Text key={goal}>{goal}</Text>)}</View>

否则,您正在做的就是尝试重新定义courseGoals.map

关于javascript - react native : functions are not valid as a react child . 如果您返回组件而不是 <component/>,则可能会发生这种情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59138704/

相关文章:

javascript - 如何在 Canvas 中获取图像的坐标x y

javascript - 无法从 DOM 获取 NAME 属性

javascript - 难以将 xstyle 加载为 dojo 依赖项

javascript - 为什么我的 useEffect() 没有在更改或页面刷新时运行 - React Js?

android - npx react-native run-android 不起作用 - "java.io.IOException: The filename, directory name, or volume label syntax is incorrect"

react-native - pod 安装给出与 ruby​​ gems 和 libffi 相关的错误

javascript - Tasker 导出的应用程序 Javascriptlet 错误

reactjs - 使用 redux-thunk 取消之前的异步操作

javascript - 用于 React Native 的 libp2p : How does libp2p get around needing NodeJS' EventEmitter when used in the browser?

reactjs - 状态改变时对话框播放动画