Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes an "React.Children.only expected to receive a single React element child." error in an apollo client rendered component

Why does adding another 2nd element to a view cause the error "React.Children.only expected to receive a single React element child."

I have a react native component that is using apollo client to call a graphQL endpoint to get data. If I have it render just a the view, it works, however when I add the touchable highlight I get the error. Can I not have more than one root element in a view, or make it a view with composite elements? I tried adding it as a Button which also threw a different error. Adding a text seems ok however.

class Games extends Component {

  constructor(props) {
    super(props);
  }

  render() {
    const { loading, allGames } = this.props.data;

    if (loading) {
      return <Text>Loading</Text>;
    } else if (allGames) {
      return (
        <View style={styles.outer}>

          //adding this touchable highlight causes the error
          <TouchableHighlight>Get More</TouchableHighlight>

          { allGames.map(game => (
            <View key={game.id} style={styles.wrapper}>
              <Text style={styles.header}>{game.title}</Text>
            </View>
          ))}
        </View>
      );
    }

    return (
      <Text>No results</Text>
    );
  }
}
Games.propTypes = {
  data: PropTypes.shape({
    loading: PropTypes.bool.isRequired,
    allGames: PropTypes.array,
  }).isRequired,
};
like image 243
MonkeyBonkey Avatar asked Nov 18 '16 17:11

MonkeyBonkey


1 Answers

According to https://facebook.github.io/react-native/docs/touchablehighlight.html,

TouchableHighlight must have one child (not zero or more than one). You can try to add a child element.

<TouchableHighlight>
  <Text>Get More</Text>
</TouchableHighlight>
like image 67
Max Avatar answered Sep 19 '22 12:09

Max