首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在不更改场景的React路由器v4本机中导航

在不更改场景的React路由器v4本机中导航
EN

Stack Overflow用户
提问于 2017-05-31 10:04:36
回答 1查看 118关注 0票数 0

我正在为React Native应用程序使用React Router。我不确定我在这里遗漏了什么,但我安装了react-router-native并需要历史包,并设置了几个方法,这些方法只是将新路由推送到堆栈上,但什么都没有发生。我点击了(‘console.log’);以检查它是否正在触发,但它不确定出了什么问题。

代码语言:javascript
复制
import React, { Component } from 'react';
import { View } from 'react-native';
import Splash from './Splash';
import createHistory from 'history/createMemoryHistory';

const history = createHistory();

class SplashContainer extends Component {
  goToLogin = () => {
    history.push('/Login');
  }
  goToRegister = () => {
    history.push('/SignUp');
  }
  render () {
    console.log(history)
    return (
      <Splash
        goToLogin={this.goToLogin}
        goToRegister={this.goToRegister}
      />
    );
  }
}

export default SplashContainer;

import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { Button } from 'native-base';
import { Link } from 'react-router-native';
import PropTypes from 'prop-types';

const Splash = (props) => {
  console.log(props)
  return (
    <View style={styles.container}>
      <Button light block onPress={props.goToLogin}>
        <Text>Login</Text>
      </Button>
      <Button dark block bordered style={{marginTop: 10}} onPress={props.goToRegister}>
        <Text>Register</Text>
      </Button>
    </View>
  );
}

Splash.propTypes = {
  goToLogin: PropTypes.func.isRequired,
  goToRegister: PropTypes.func.isRequired
}

export default Splash;
EN

回答 1

Stack Overflow用户

发布于 2017-10-11 03:38:59

我不知道您的路由器配置,但您的方法应该是:

代码语言:javascript
复制
goToLogin = () => {
    const { history } = this.props
    history.push('/Login');
}

history将通过路由器堆栈中的组件道具向下传递。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44274502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档