黑屏,反应本机导航,无错误代码

时间:2019-02-11 18:47:00

标签: react-native axios react-navigation

请查看调试器和屏幕上可能存在的问题。但是,下面将高度显示该代码供您细读。 另外,我旨在基于所选内容的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

我尝试删除alignItem = center。实际上,我删除了我的样式,以查看是否有任何阻碍屏幕显示的内容。

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

});

4 个答案:

答案 0 :(得分:1)

我没有测试此代码,但是尝试将flex: 1添加到您的容器样式中。如果不告诉他们,主要的容器/组件就不会拉伸

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

此外,要检查组件是否渲染(有助于调试问题所在),请在每个组件的componentDidMount中编写控制台日志。如果它们安装了,但看不到任何东西,则很可能是CSS问题。如果不是,它将引发错误而不是空白屏幕

第二个问题是,当您进行导航时,您需要使用带有react-navigation的参数。语法如下:

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

因此,如果您的参数中有{id:someId},则在导航到的组件中将有{this.props.navigation.state.params.id}。所以基本上这些参数都在navigation.state.params内进行导航

答案 1 :(得分: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>
        );
    }

答案 2 :(得分:1)

让我帮助您解决第二个问题。首先,只需在导航中指定参数即可轻松实现。 例如,

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

我希望对您有帮助

答案 3 :(得分:0)

如果您使用的是 react-navigation 5.x,那么您可能正在添加 CSS

alignItems: "center"

到您的根文件,即 App.jsindex.js 我刚刚删除它,它开始为我工作。