刚开始学习本机,

我创建了一个单独的文件flexdemo.js并创建了如下组件:

import React, { Component } from 'react';
import { View } from 'react-native';

export default class FlexibleViews extends Component {
    render() {
        return (
            <View style={{ flex: 1 }}>
                <View style={{ flex: 1, backgroundColor: "powderblue" }}> </View>
                <View style={{ flex: 2, backgroundColor: "skyblue" }}> </View>
                <View style={{ flex: 3, backgroundColor: "steelblue" }}> </View>
            </View>

        );
    }
}

和App.js文件如下:
import React, { Component } from 'react';
import {
  AppRegistry,
  Platform,
  StyleSheet,
  Text,
  View, Image
} from 'react-native';

// import Bananas from './src/banana';
// import LotsOfStyles from './src/styledemo'

import FlexibleViews from './src/flexdemo';

export default class App extends Component {
  render() {
    return (
      // <Bananas name = "Tapan"/>
      <View>
        <FlexibleViews />
      </View>

    );
  }
}

这给了我这个错误:

react-native - React native:无法添加没有YogaNode或父节点的子节点-LMLPHP

现在,如果我尝试通过将flexdemo.js代码添加到App.js中来运行代码,则一切正常。

像这样更改了App.js:
import React, { Component } from 'react';
import { AppRegistry, View } from 'react-native';

export default class FlexDimensionsBasics extends Component {
  render() {
    return (
      // Try removing the `flex: 1` on the parent View.
      // The parent will not have dimensions, so the children can't expand.
      // What if you add `height: 300` instead of `flex: 1`?
      <View style={{flex: 1}}>
        <View style={{flex: 1, backgroundColor: 'powderblue'}} />
        <View style={{flex: 2, backgroundColor: 'skyblue'}} />
        <View style={{flex: 3, backgroundColor: 'steelblue'}} />
      </View>
    );
  }
}

react-native - React native:无法添加没有YogaNode或父节点的子节点-LMLPHP

最佳答案

删除组件内部的注释。

关于react-native - React native:无法添加没有YogaNode或父节点的子节点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46605376/

10-09 07:11
查看更多