【问题标题】:Cannot add a child that doesn't have a YogaNode to parent无法将没有 YogaNode 的子代添加到父代
【发布时间】:2020-02-15 07:15:43
【问题描述】:

“无法将没有 YogaNode 的子节点添加到没有测量功能的父节点!” React-Native Expo 中的错误。我相信在一个组件中与我的地图有关,我只是返回一个带有信息的 h2 标签,然后我意识到它需要是一个文本,而是将它放在一个文本中并得到这个新错误。网上的人说它也需要是一个视图,但这似乎并没有解决它。

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

export default function ListTasks(props) {
  return (
    <View style={styles.container}>
      <Text>
        {" "}
        {props.tasks.map(task => {
          return (
            <View style={styles.container}>
              <Text>{task.Task}</Text>
            </View>
          );
        })}{" "}
      </Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  }
});



export default class TodoList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      tasks: [
        { Completed: false, Task: "Drink water" },
        { Completed: false, Task: "Work on React-Native app" },
        { Completed: false, Task: "Clean bedroom" }
      ]
    };
  }
  render() {
    return (
      <View style={styles.container}>
        <ListTasks tasks={this.state.tasks} />
      </View>
    );
  }
}

【问题讨论】:

  • 我相信花括号 {" "} 会导致问题。我可以知道这些包裹地图的大括号的用途吗?
  • 这只是更漂亮的自动执行的操作,当我删除它们时它不会改变任何东西。
  • 尝试使用this.props访问props,即this.props.tasks并删除类名旁边的单词props
  • 如果你这样做你会得到一个 TypeError,this.props.tasks 是未定义的

标签: javascript reactjs react-native


【解决方案1】:

这是简单的“;”在返回的最后,更漂亮的是自动放入的地图..

<Text>
        {" "}
        {props.tasks.map(task => {
          return (
            <View style={styles.container}>
              <Text>{task.Task}</Text>
            </View>
          )
        })}{" "}
      </Text>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 2018-06-29
    • 2019-01-19
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多