axios 带有react-native导航的空白屏幕,无错误代码

6fe3ivhb  于 2023-10-18  发布在  iOS
关注(0)|答案(5)|浏览(122)

请查看调试器和屏幕上可能存在的问题。然而,代码高度显示在下面供您阅读。此外,我的目标是根据所选内容的ID导航到另一个页面。
App.js

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
我试着删除了Item = 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({

});
3ks5zfa0

3ks5zfa01#

我没有测试这段代码,但尝试将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中,

x6492ojm

x6492ojm2#

我来帮你回答第二个问题。首先,正如前面所说的,只需要在导航中指定参数就可以了。比如说,

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

我希望它能帮助

0yg35tkg

0yg35tkg3#

我建议使用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>
        );
    }
bkhjykvo

bkhjykvo4#

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

alignItems: "center"

到你的根文件,即。* app.js * 或 * index.js * 我只是删除它,它开始为我工作。

shyt4zoc

shyt4zoc5#

我遇到了同样的问题,没有解决办法。经过一些调试,我找到了导致问题的原因。这是我在App.js文件中调用AppNavigation组件的旧代码。

  • 旧代码:*
export default function App() {
        return (
          <View style={styles.container}>
            <AppNavigation/>
            <StatusBar style="auto" />
         </View>
            );
        }

这是我更新后的App.js文件,其中我删除了所有内容返回了AppNavigation组件。

  • 新代码:*
export default function App() {
      return (
        <AppNavigation/>
      );
    }

我希望这对某人有所帮助。

相关问题