简体   繁体   English

React Native中的Stack Navigator

[英]Stack Navigator in react native

I am using stack navigator to navigate between screen in my react native app. 我正在使用堆栈导航器在我的本机应用程序的屏幕之间导航。 But it is giving me an error using this.props.navigation.navigate . 但是使用this.props.navigation.navigate给我一个错误。 kindly tell me what i am doing wrong here . 请告诉我我在这里做错了什么。 输出图像

I am using react-native router-flux too in my app but for some purpose i need to use stack navigator as my app has home page and it shows some content of blog feed and onclicking read more it should open detailed view of only that specific blog feed in detailed page, therefore, for this purpose i am using stack navigator here . 我在我的应用程序中也使用了react-native路由器流量,但出于某种目的,我需要使用堆栈导航器,因为我的应用程序具有主页,并且显示了博客供稿的一些内容,并单击了其中的更多内容,应仅打开该特定内容的详细视图详细页面中的博客提要,因此,为此目的,我在此处使用堆栈导航器。 Here is my code: 这是我的代码:

Home.js: Home.js:

import React, { Component } from 'react';
import {
  ActivityIndicator,
  ListView,
  Text,
  StyleSheet,
  View,
  Button,
  TouchableOpacity
} from 'react-native';
import {StackNavigator} from 'react-navigation';
import {Actions, Router, Scene} from 'react-native-router-flux';
import TimeAgo from 'react-native-timeago';

import {
  Card,
  CardItem,
  Content,
  Header,
  Body,
  Footer,
  FooterTab,
  Container,
  Left,
  Icon,
  Right
} from 'native-base';

import {GetImage,ContentSnippet} from './helpers/helpers';
import HTMLView from 'react-native-htmlview';
import FitImage from 'react-native-fit-image';

export default class Home extends Component {


  constructor(props) {
    super(props);
    this.state = {
      isLoading: true,
      count:0,
      count1:0
    }
  }


  componentDidMount() {
    return fetch('http://www.cardory.co.uk/jan/json')
      .then((response) => response.json())
      .then((responseJson) => {
        let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !==     r2});
        this.setState({
          isLoading: false,
          dataSource: ds.cloneWithRows(responseJson.items),
        }, function() {
          // do something with new state
        });
      })
      .catch((error) => {
        console.error(error);
      });
  }


  render() {
    if (this.state.isLoading) {
      return (
        <View style={{flex: 1, paddingTop: 80}}>
          <ActivityIndicator />
        </View>
      );
    }
    return (
      <Container style={{flex: 1, paddingTop: 60}} >
      <ListView
        dataSource={this.state.dataSource}
        renderRow={(rowData) =>

      <Card>
        <CardItem header>
          <Text style={styles.titleHeading}>{rowData.title}</Text>
        </CardItem>
        <CardItem cardBody>
            <Content style={{marginLeft:10,marginRight:10}}>
              <FitImage source={{uri: GetImage(rowData.content_html)}}/>
              <HTMLView value={ContentSnippet(rowData.content_html.replace(/<img[^>]*>/g,""))}/>
            </Content>
        </CardItem>
        <CardItem>
            <Left>

              <TouchableOpacity onPress={() =>this.props.navigation.navigate('Detail')}>
                <Text style={styles.ReadMore}>
                  Read more
                </Text>
              </TouchableOpacity>
            </Left>

            <Right>
              <Text style={styles.Time}>
                <Icon name="time" style={{color:'#483D8B'}}/>
                <TimeAgo time=  {rowData.date_published}/>
              </Text>
            </Right>
        </CardItem>

      </Card>
    }
  />
  </Container>
);
  }

}



const styles=StyleSheet.create({
  textHeading:{
    fontSize:20,
    marginTop:20
  },
  titleHeading:{
    fontSize:20,
    fontWeight:'bold',
    color:'black',
    alignItems:'center',
  },
  ReadMore:{
    color:'#483D8B',
    fontSize:15,
    fontWeight:'bold'
  },
  Time:{
    color:'#483D8B',
    alignSelf:'stretch'
  },
});

Navigation.js: Navigation.js:

import React,{Component} from 'react';

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

import {StackNavigator} from 'react-navigation';
import Home from './Home';
import Info from './Info';
import Details from './Details';
import Register from './Register';

const Navigation = StackNavigator({
    Home:{screen: Home},
    Info:{ screen: Info},
    Details:{ screen: Details},
    Register:{ screen: Register}
});

And my aim is on cliking read more it should open only that specific blog feed any help would be appreciated. 我的目的是希望获得更多阅读,只有在打开特定博客供稿时,任何帮助才会受到赞赏。 Thanks in advance! 提前致谢!

in onPress callback, this may be changed, so the value of this.props.navigation may be different to your expect. onPress回调中, this可能会更改,因此this.props.navigation的值可能与您的预期不同。 you should use a variable to save navigation and avoid use this in callback. 您应该使用变量保存navigation并避免在回调中使用this变量。 see my code below. 请参阅下面的代码。

render() {
  const {navigation} = this.props;
  ...
  <TouchableOpacity onPress={() => navigation.navigate('Detail')}>
  ...
}

I believe what @Harlan is asking for is something like: 我相信@Harlan要求的是这样的:

    import AppNavigator from './Navigation.js'

    class App extends Component {
       render(){
         return(
           <View>
            <AppNavigator/>
           </View>
         )
       }
    }

    AppRegistry.registerComponent( 'app', () => App );

And he probably has a point since you are not exporting your navigator in Navigation.js. 他可能有一点要说,因为您没有在Navigation.js中导出导航器。 Without something like the above code, then your components will not have navigation in their props. 如果没有类似上面的代码,那么您的组件将无法在其道具中导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM