S. M. Asif
S. M. Asif

Reputation: 3709

How to solve Invarient violation:Text strings must be rendered within a <Text> component in React Native?

In my React- Native project I have installed the following version of react-navigation:

npm install react-navigation@^1.0.0-beta.11

And then run the command:

npm install

After all these installation I have created a class named WelcomeScreen.js and here is the code given below for that-

WelcomeScreen.js

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

class WelcomeScreen extends Component {

    static navigationOptions = {
        header: 'none'
    }

    render() {
        return (
            <View style={styles.container}>
                <Button title="Log in" onPress={() => this.props.navigation.navigate('LoginScreen')}/>
                <Button title="Sign Up" onPress={() => this.props.navigation.navigate('SignUpScreen')}/>
            </View>
        )
    }
}
export default WelcomeScreen;

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center'
    }
});

After that, I have routed this class in my App.js file. Here is the code given below:

App.js

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

import { StackNavigator } from 'react-navigation'
import WelcomeScreen from './screens/WelcomeScreen'
export default class App extends React.Component {
  render() {
    return (
      <AppStackNavigator/>
    );
  }
}

const AppStackNavigator = new StackNavigator({
  WelcomeScreen: { screen: WelcomeScreen }
})


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

Then after running my project, I am keep getting this error:

Invarient violation:Text strings must be rendered within a component

Invarient violation:Text strings must be rendered within a component

I am not understanding why I am getting this error ? I have tried following solutions-

Invariant Violation: Text strings must be rendered within a <Text> component

https://github.com/facebook/react-native/issues/20084

None of them were helpful for me. So, it would be very nice if someone help me out with this.

Upvotes: 1

Views: 2851

Answers (3)

Peter S.
Peter S.

Reputation: 131

I'd consider working with React's newest navigation package react-navigation-stack (currently in 2.0 alpha - link).

You would implement this exactly like previous react-navigation stack navigators.

import React from 'react'
// ...
import { createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'

const HomeStack = createStackNavigator(
  { HomeScreen, AboutScreen },
  { initialRouteName: "HomeScreen" }
)

HomeStack.navigationOptions = {
  headerShown: false, // a more concise option, to avoid future deprecation.
}
// ... continues in next code block

Don't forget to wrap your application in a react-navigation AppContainer too.

// ...
const AppNav = createAppContainer(HomeStack)

export default AppNav

Of course, if you're modifying navigationOptions in other screens, you would simply attach the alternate options within that screen component:

class HomeScreen extends React.Component {
    static navigationOptions = {
        headerShow: false        
    }
    render() { 
        return (<View><Text>Welcome home.</Text></View>)
    }
}

As seen above, we can now use the navigation option: headerShown: false.

With this new package, you'll be preparing your application for the future iterations of React's navigation packages.


Note ;'s left out as a styling choice.

Upvotes: 0

Paras Korat
Paras Korat

Reputation: 2065

You have used a wrong value in a header

static navigationOptions = {
    header: "none"        
};

Replace "none" with null

static navigationOptions = {
        header: null        
};

Upvotes: 3

Odog8
Odog8

Reputation: 49

    const AppStackNavigator = new StackNavigator({
  WelcomeScreen: { screen: WelcomeScreen }
})


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

is it this comma?

Upvotes: 1

Related Questions