TapanHP
TapanHP

Reputation: 6191

React native: Cannot add a child that doesn't have a YogaNode or parent node

Just started learning react-native,

I have created one separate file flexdemo.js and created component as below:

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>

        );
    }
}

and App.js file is as below:

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>

    );
  }
}

That gives me this error:

enter image description here

Now if I try to run the code by adding flexdemo.js code into App.js then everything works fine.

Changed The App.js like this:

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>
    );
  }
}

enter image description here

Upvotes: 42

Views: 27871

Answers (14)

ketan
ketan

Reputation: 1

In my case I had written <TextInput> in the <Text> tag. Below is wrong. It will give error for child.

<text>
  <TextInput style={styles.textinput} 
        textcolor
          placeholder = 'user id'
          placeholderTextColor = 'gray'
   />
</Text>

solution.

 <Text> hello </Text>
 <TextInput style={styles.textinput} 
        textcolor
          placeholder = 'user id'
          placeholderTextColor = 'gray'
  />

you have to write separate tag.

So any tag you have written in another tag this error can come.

Upvotes: 0

Soner PALANCI
Soner PALANCI

Reputation: 114

        <View style={{ flex: 1 }}>
            <Text style={{ flex: 1, backgroundColor: "powderblue" }}> </Text>
            <Text style={{ flex: 2, backgroundColor: "skyblue" }}> </Text>
            <Text style={{ flex: 3, backgroundColor: "steelblue" }}> </Text>
        </View>
  1. Make use Component hierarchy should be maintain, for example all components like Text, ListView, TextInput etc should be wrapped inside the parent component that is View. lets see the below example :

    < View >
    < Text >
    CORRECT
    < / Text >
    < / View >

  2. Make sure all the Component tag should be closed properly.

  3. Make sure unnecessary semicolons should be removed from the react native layout components & functions.

https://www.skptricks.com/2018/08/react-native-cannot-add-child-that.html

Upvotes: 2

Pauloco
Pauloco

Reputation: 1099

I want to give a more general answer here, because there can be several reasons for the issue returning the same error message. The three I have seen the most:

1. Comments might be the cause. But instead of removing comments make them work:

In the return()-part, variables need to be wrapped in {} like {this.state.foo} so wrapping the comments works fine...

    return(
        <Text> This works {/* it really does */}</Text>
    );

...as long as they are not the first or last element in the return statement:

    return(
      {/* This fails */}
      <Text> because the comment is in the beginning or end </Text>
      {/* This also fails */}
    );

2. Conditional rendering might be the cause. If myCheck is undefined or an empty string this can fail:

    const myCheck = ""; /* or const myCheck = undefined */
    return(
      {myCheck && <MyComponent />}
    );

but adding double negation !! works:

    const myCheck = ""; /* or const myCheck = undefined */
    return(
      {!!myCheck && <MyComponent />}
    );

3. Whitespaces (or actually any strings) within a component can cause this, if not in a <Text>-Component:

Text in a View for example:

    /* This fails */
    return(
      <View>it really does</View>
    );

But also the tiny space between two components:

    /* <View>*Space*<Text> fails: */
    return(
      <View> <Text>it really does</Text> </View>
    );

But works if in a newline:

    return(
      <View>
        {/* This works */}
        <Text>surprisingly it does</Text>
      </View>
    );

Unfortunately these pitfalls do not always lead to errors. Sometimes they work. I guess this depends on which of all those many tools/libraries/components you use and their versions in your app.

Upvotes: 40

TacoEater
TacoEater

Reputation: 2278

In my case I had a condition in my render function that resulted in evaluating 0.

It seems that 0 && 'some jsx' breaks in newer versions of react native.

Error Example:

render(){
   return <View>
              {this.state.someArray.length && <View>...</View>}
      </View>
}

Although this should be valid javascript and works in react since 0 is falsey, It crashes in react native, not sure why but it works with a little refactoring as:

Working Example:

render(){
   return <View>
              {this.state.someArray && this.state.someArray.length> 0 && 
              <View>...</View>}
          </View>
}

Upvotes: 1

Adnan
Adnan

Reputation: 5075

In my case I had small () brackets around one of my view which was causing error.

({renderProgress()})

Removing small brackets worked for me.

{renderProgress()}

Upvotes: 1

Nickmccomb
Nickmccomb

Reputation: 1527

This error also occurs if you have comments in your render() return() function. Remove all comments in your return function when rendering JSX

Upvotes: 0

Khan Shahrukh
Khan Shahrukh

Reputation: 6401

I have encountered the same issue just now and solved it by removing the comments that I have made while editing the project in android studio and over there the comment's shorotcut just adds /* and */ but actually for react native the commented code should be enclosed with starting and end of curly braces, for example following would be an invalid comment:

/*<Text style={styles.pTop}>
 {
    this.state.response.map((index, value) => {
    return index.title;
    })
 }
 </Text>*/

And the following will be a valid one:

{/*<Text style={styles.pTop}>
 {
    this.state.response.map((index, value) => {
    return index.title;
    })
 }
 </Text>*/}

you see there is just one minor difference of enclosing the comment in curly braces.

Upvotes: 0

MOHAMMED BASHEER T
MOHAMMED BASHEER T

Reputation: 1

Remove semi-colon when rendering a method in

<View style={styles.container}> {this.renderInitialView()} //semi-color should not be here </View>

Upvotes: 0

S.Hossein Asadollahi
S.Hossein Asadollahi

Reputation: 1650

If you have if else statement in your render() function use !! like this:

{!! (this.state.your_state) &&
   <View>
        <Text>Your Text</Text>
   </View>
}

instead of:

{(this.state.your_state) &&
    <View>
         <Text>Your Text</Text>
    </View>
}

Upvotes: 3

yubinvip
yubinvip

Reputation: 11

Delete the comment in the return block "// " I encountered the same problem when I accidentally add a ';' in the return block, the iOS is work well, but the Android has this bug information

Upvotes: 1

Rohith M
Rohith M

Reputation: 31

This error is usually from one of below mistakes

  • Remove unnecessary comments and remove comments from return function.
  • check for proper variable name.
  • check for unintended semicolon or any wrong syntax

Upvotes: 1

Roshan Salian
Roshan Salian

Reputation: 551

Remove comments inside component.

Upvotes: 41

I was able to reproduce the issue with the code you provided. The solution is twofold:

  1. In your flexdemo.js file you should remove the whitespaces from within the <View> tags. They are considered as text, and text is only allowed inside a <Text> component. I'd recommend making your <View> tags self closing until they have some content, to stay away from this issue in the future, like so:

    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 style={{ flex: 2, backgroundColor: 'skyblue' }} />
            <View style={{ flex: 3, backgroundColor: 'steelblue' }} />
          </View>
        );
      }
    }
    

    This will render your components but still be faulty, as you wont see anything on the screen.

  2. To get your flexible shades of blue to appear you'll either have to add flex to the <View> component in your App.js file or(depending on what your next steps are, I guess) remove it and render your <FlexibleViews> as the root component, since it is basically a <View> component with some children anyway.

Upvotes: 10

pedrommuller
pedrommuller

Reputation: 16066

I downgrade react native version, then I got a different error, basically what it was I had a simple string within a view, something like this:

<View>
    MyComponent
</View>

I had to wrap the string with a text component like this:

<View>
    <Text>MyComponent</Text>
</View>

hope that helps

Upvotes: 2

Related Questions