【问题标题】:Navigate between different components React Native在不同组件之间导航 React Native
【发布时间】:2021-01-28 18:48:36
【问题描述】:

所以我一直在尝试了解如何执行此操作 2 小时,但我似乎无法弄清楚。单击按钮时,我希望能够从一个组件转到另一个组件。

First.js

import React, {useState} from 'react';
import { StyleSheet, StatusBar, SafeAreaView, View, Text } from 'react-native';
import {Button} from 'react-native-elements';

import { Second } from './Second.js';


export class Information extends React.Component {
    
    render() {
        return (
        <SafeAreaView style={styles.container}>
                <View style={styles.footer}>
                    <View style={styles.footerBtn}>
                    <Button 
                        titleStyle={{
                            fontSize: 16,
                        }}
                        buttonStyle={{
                            backgroundColor: '#007AFF'
                        }}
                        raised={true}
                        title="Continue"
                        onPress={() => { this.props.navigation.navigate(Second) }}
                        color="#007AFF"
                    />
                    </View>
                </View>
        </SafeAreaView>
        );
    }
}

Second.js

import React from 'react';
import { StyleSheet, StatusBar, SafeAreaView, Dimensions, View, Text } from 'react-native';

export class Postcode extends React.Component {
    render() {
        return (
        <SafeAreaView style={styles.container}>
            <StatusBar />
            <Text> Wow this is a cool second page</Text>
        </SafeAreaView>
        );
    }
}

所以我用所有额外的东西删掉了我的一些代码,但上面是两个基本文件。它们都在同一个文件夹中,当我单击按钮时,我希望能够从第一页转到第二页。我觉得我很笨,答案很明显,但我就是想不通。

【问题讨论】:

  • 您需要将您在navigate() 方法而不是组件中提供的名称/密钥传递给第二个屏幕。喜欢这个this.props.navigation.navigate('Second')

标签: javascript reactjs react-native react-navigation


【解决方案1】:

我认为最好的方法是使用堆栈导航,例如我拥有的项目示例:

栈组件:

  import React from 'react';

  import Home from '../pages/Home';
  import Sales from '../pages/Sales';
  import NewSale from '../pages/Sales/NewSale';

  import { createStackNavigator } from '@react-navigation/stack';

  const Stack = createStackNavigator();

  function Stacks() {
    return (
      <Stack.Navigator>
        <Stack.Screen name='Home' component={Home} options={{ headerShown: false }} />
        <Stack.Screen name='Negociação' component={Sales} />
        <Stack.Screen name='Nova Negociação' component={NewSale} />
      </Stack.Navigator>
    );
  }

  export default Stacks;

点击按钮进行导航的位置:

import React from 'react';
import * as S from './styles';

export default function Sales({ navigation }) {
    return (
        <S.Container>

            <S.Add
                onPress={() => navigation.navigate('Nova Negociação')}>
            </S.Add>

        </S.Container>
    )
}

app.tsxapp.js

import 'react-native-gesture-handler';

import { NavigationContainer } from '@react-navigation/native';
import React from 'react';
import { StatusBar } from 'react-native';
import styles from './styles/styles';

import Stacks from './stackNav';

const App: React.FC = () => (
  <NavigationContainer>
    <StatusBar barStyle="light-content" backgroundColor={styles.primaryColor} />
    <Stacks />
  </NavigationContainer>
);

export default App;

编辑:查看这个世博小吃:https://snack.expo.dev/@guilhermemoretto12/react-native-stack-navigator-example

【讨论】:

  • 这对我来说很难理解。能否请您标记每个代码 sn-p 的文件名或在snack.expo.dev 创建一个功能示例?
  • @velkoon 在这里,我让它尽可能简单:snack.expo.dev/@guilhermemoretto12/…
  • 谢谢你,你真好。在在线向许多人寻求帮助后,我最终在当天工作了 8 小时后,终于让它工作了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-06
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
相关资源
最近更新 更多