空白屏幕与没有错误码反应本地导航

问题描述 投票:1回答:3

请看看调试器,什么可能是一个问题的屏幕。但是,代码是高度显示如下,请过目。也比较多,我瞄准导航到基于所选内容的ID另一页。

App.js enter image description here的App.js是我定义我的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

我曾试图删除对齐项=中心。事实上,我删除了我的风格,看看是否有任何来自上来挡住了屏幕。

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({

});
react-native axios react-navigation
3个回答
1
投票

让我来帮助你与你的第二个问题。首先,如刚刚在您的导航指定PARAMS说,这是更容易。例如,

readMore = (id) => {

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

     } 

然而,在你的TouchableOpacity,onPress方法即onPress = {()=> this.readMore(post.id)}

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

我希望它能帮助


2
投票

我没有测试此代码,但尽量flex: 1添加到您的容器风格。主要的集装箱/如果你不告诉他们到组件不舒展

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

同时,以检查组件渲染(帮助调试问题的所在),写一个控制台日志中的每个组件的componentDidMount。如果他们安装,但没有什么是可见的,这是最有可能是CSS的问题。如果不是,它会引发错误,而不是黑屏

第二个问题是,当你浏览你需要有反应的导航PARAMS。它的语法是这样的:

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

所以,如果你有{ID:someId}在您的参数,可以在组件您导航到你将有{this.props.navigation.state.params.id}。所以基本上这些PARAMS,在那里你导航里面navigation.state.params


1
投票

我会建议使用flaltist这一点,而不是this.state.map。这应该给你相同的结果

readMore(id){
 //do whatever you want with the id
       this.props.navigation.navigate('PostSingleScreen',{id:id}); //or pass it as a prop
       debugger;
     } 

renderItem = ({ item, index }) => {
    return (
       <View key={index} style={styles.container}>
                        <Text style={styles.display}>
                            Author:  {item.author}
                        </Text>
                        <Text style={styles.display}>
                            Category: {item.category_name}
                        </Text>
                        <Text style={styles.display}>
                            Title: {item.title}
                        </Text>
                        <Text style={{overflow:'hidden'}}>
                            Id: {item.id}
                        </Text>
                     <TouchableOpacity style={styles.buttonContainer}
                     onPress = {() => this.readMore(item.id)}
                     >
                        <Text style={styles.buttonText}>

                            Read More
                        </Text>
                     </TouchableOpacity>

                     </View> 
    );
  };

render(){
        return (
        <View style={{flex:1}}>
         <FlatList
          style={{flex:1}}
          data={this.state.posts}
          renderItem={this.renderItem}
          numColumns={1} 
          keyExtractor={(item, index) => item.id} //this needs to be a unique id
          ListHeaderComponent = {
            <View style={styles.header}>
                <Text style={styles.headerText}>Gist Monger</Text>
            </View>}
        />
        <View style={styles.footer}/>
        </View>
        );
    }
© www.soinside.com 2019 - 2024. All rights reserved.