gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:23:38 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com