2

I'm using react-native-cli version 2.0.1 and react-native version 0.57.8. I am learning React native, and have encountered an issue when trying to render two different child components (Header and AlbumList) in the main App component.

Issue

enter image description here

The error disappears if I remove the <View> tags and AlbumList component in the index.js file (meaning only show one component). I have looked through threads like this, but am still unable to identify how I'm using the <View> tag incorrectly.

index.js

import React from 'react';
import { View, Text, AppRegistry } from 'react-native';
import Header from './src/components/Header';
import AlbumList from './src/components/AlbumList';

//>Create a component
const App = () => (
<View> <Header headerName={'Albums'} /> 
<AlbumList />
</View>

);


//>Render component on device
AppRegistry.registerComponent('albums', ()=> App);

AlbumList.js

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

const AlbumList = () => {
return (
  <View><Text> Album List </Text></View>
);
};

export default AlbumList;

I think the issue isn't anything to do with the Header.js file, but sharing code nonetheless.

Header.js

import React from 'react';
import { Text, View } from 'react-native'; // The view tag allows us to position and wrap elements

// Make a component
const Header = (props) => {

  const { textStyle, viewStyle } = styles;

  return (
    <View style = {viewStyle}>
      <Text style = {textStyle}> {props.headerName}</Text>
    </View>
  );
};

const styles = {
  viewStyle: {
      backgroundColor: '#F8F8F8',
      alignItems: 'center',
      justifyContent: 'center',
      height: 60
  },

  textStyle: {
    fontSize: 20
  }
};

export default Header;

Help is appreciated. Thanks in advance!

2
  • what did you write in the component/index.js ? Commented Dec 27, 2018 at 4:16
  • I have an index.js in the root, and its contents are in the post. Commented Dec 27, 2018 at 4:17

2 Answers 2

5

in your index.js file, replace App function with below,

//Create a component
const App = () => (
  <View><Header headerName={'Albums'} /><AlbumList /></View>
);

there should be no space between components here

while using react native components, try to put each element in a new line, in this way you don't have to worry about spaces

const App = () => (
      <View>
        <Header headerName={'Albums'} />
        <AlbumList />
      </View>
    );

Let me know if it works

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! Do you know what the syntax would be if I didnt want to worry about spaces? I tried this and it didnt work - const App = () => { return ( <View> <Header headerName={'Albums'} /> <AlbumList /> </View> ); };
@KeshavVasudevan, I updated the answer with the preferred syntax. Check it out.
Thanks a ton @farid!
2

In my case i had a misplaced semicolon as shown and it was giving me so much headache

 <Screen>
        <RestaurantScreen />; //Make sure you have no misplaced semicolon like in my case here
      </Screen>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.