Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank Screen with react-native navigation with no error code

Please, look at the debugger and the screen for what could be a problem. However, the code is highly displayed below for your perusal. More also, I aimed at navigating to another page based on the id of the selected content.

App.js enter image description here The App.js is where I defined my stackNavigator

import React, {Component} from 'react';
import { StyleSheet, Text, View} from 'react-native';
import Post from './components/Post';
import PostSingle from './components/PostSingle';
import { createStackNavigator, createAppContainer } from 'react-navigation';

const RootStack = createStackNavigator(
  {
    PostScreen: { screen: Post},
    PostSingleScreen:{screen: PostSingle},

  }, 
  {
    initialRouteName: "PostScreen"
  }
);

const AppNavigator = createAppContainer(RootStack);
export default class App extends Component {
  constructor(props) {
    super(props);
  };
  render() {
    return (
      <View style={styles.container}>

        <AppNavigator/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#F3F3F3',
  }
});

Post.js

I have tried to delete alignItem = center. In fact I deleted my style to see if there is any blocking the screen from coming up.

import React, { Component } from 'react';
import {
    ScrollView, 
    StyleSheet,
    View, 
    Text,
    InputText,
    TouchableOpacity
} from 'react-native';
import axios from 'axios';

export default class Post extends Component{
    constructor(props){
        super(props);
        this.state = {
            posts: []
        }
    }
     readMore = () => {

       ()=> this.props.navigation.navigate('PostSingleScreen');
       debugger;
     } 

    componentDidMount(){
        axios.get(`http://localhost/rest_api_myblog/api/post/read.php`)
        //.then(json => console.log(json.data.data[0].id))
        .then(json => json.data.data.map(mydata =>(
            {
                title: mydata.title,
                body: mydata.body,
                author: mydata.author,
                category_name: mydata.category_name, 
                id: mydata.id 
            }
        )))
        //.then(newData => console.log(newData))
       .then(newData => this.setState({posts: newData}))
       .catch(error => alert(error))

        }

    render(){
        return (
        <View>

        <ScrollView style={styles.scrollContent}>
            <View style={styles.header}>
                <Text style={styles.headerText}>Gist Monger</Text>
            </View> 
             {   
                 this.state.posts.map((post, index) =>(
                    <View key={index} style={styles.container}>
                        <Text style={styles.display}>
                            Author:  {post.author}
                        </Text>
                        <Text style={styles.display}>
                            Category: {post.category_name}
                        </Text>
                        <Text style={styles.display}>
                            Title: {post.title}
                        </Text>
                        <Text style={{overflow:'hidden'}}>
                            Id: {post.id}
                        </Text>
                     <TouchableOpacity style={styles.buttonContainer}
                     onPress = {() => this.readMore()}
                     >
                        <Text style={styles.buttonText}>

                            Read More
                        </Text>
                     </TouchableOpacity>

                     </View> 
                 ))
             }

        </ScrollView>
            <View style={styles.footer}></View>
        </View>
        );
    }
}

const styles = StyleSheet.create({

 header: {
     flex: 1,
     height:40,
     marginTop:50,
     marginBottom:10,
     flexDirection: 'row', 
     justifyContent:'center',


 },
display: {
   margin: 3,
   fontSize: 16
}, 

headerText: {
    fontWeight: 'bold', 
    fontSize: 40,
    color: '#6200EE'
},

 container: {
    backgroundColor:'#efefef',
    padding: 20,
    margin: 5,
    borderRadius:20,

    justifyContent: 'center', 
    alignItems: 'center'
},
footer: {
    flex: 1,
    backgroundColor:'#000',
    marginBottom:50
}, 
buttonContainer:{
    height: 30,
    width: 200,
    marginTop: 15,
    justifyContent: 'center', 
    alignItems: 'center',
    borderRadius: 15,
    backgroundColor:'#6200EE'
},
buttonText: {
alignContent: 'center',
color: 'white'
}
});

PostSingle.js

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

} from 'react-native';
import axios from 'axios';

export default class Post extends Component{
    constructor(props){
        super(props);

    }



    render(){
        return (
        <View>
           <Text>My text</Text>
        </View>
        );
    }
}

const styles = StyleSheet.create({

});
like image 613
Payne Avatar asked Oct 28 '25 21:10

Payne


2 Answers

I did not test this code, but try to add flex: 1 to your container style. the main containers/components don't stretch if you don't tell them to

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#F3F3F3',
    flex: 1,
  }
});

also, to check if the components render (helps debugging where the problem is), write a console log in every component's componentDidMount. if they mount, but nothing is visible, it's most likely a CSS issue. If it wasn't, it would throw errors instead of blank screen

second issue is, when you navigate you need to have params with react-navigation. the syntax for it is like this:

this.props.navigation.navigate('PostSingleScreen', { params })

so, if you have { id: someId } in your params, in the component you navigated to you will have {this.props.navigation.state.params.id}. so basically those params are inside navigation.state.params where you navigate

like image 108
Luna Avatar answered Oct 30 '25 15:10

Luna


Let me help you with your second question. First, it is easier as said by just specifying params in your navigation. For instance,

readMore = (id) => {

    this.props.navigation.navigate('PostSingleScreen', {id:id})

     } 

However, in your TouchableOpacity, onPress method i.e. onPress = {() => this.readMore(post.id)}

In your PostSingle.js

import React, { Component } from 'react';
import {
    StyleSheet,
    View, 
    Text,
    Button

} from 'react-native';

import axios from 'axios';

class PostSingle extends Component{
    constructor(props){
        super(props);
        this.state = {
            posts: []
        }
    }


 componentDidMount() {
    const id = this.props.navigation.state.params.id;
    axios.get(`http://localhost/rest_api_myblog/api/post/read_single.php?id=${id}`)
    .then(json => json.data)
   .then(newData => this.setState({posts: newData}))
   .catch(error => alert(error))

 }  
    render(){
        return (

        <View style={styles.container}>
              <Text style={styles.display}>
                           {this.state.posts.title}
              </Text>
              <Text style={styles.display}>
                            {this.state.posts.author}
              </Text>
              <Text style={styles.display}>
                            {this.state.posts.category_name}
              </Text>
              <Text style={styles.display}>
                          {this.state.posts.body}
              </Text>  
        </View>
        );
    }
}

I hope it helps

like image 44
Abdulkabir Ojulari Avatar answered Oct 30 '25 13:10

Abdulkabir Ojulari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!