简体   繁体   English

在不同组件之间导航 React Native

[英]Navigate between different components React Native

So I've been trying to understand how to do this for like 2 hours now but I can't seem to figure it out.所以我一直在尝试了解如何执行此操作大约 2 个小时,但我似乎无法弄清楚。 I want to be able to go from one component to another when the button is clicked.单击按钮时,我希望能够 go 从一个组件到另一个组件。

First.js首先.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第二.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>
        );
    }
}

So I cut out some of my code with all the extra stuff but above is the two basic files.所以我用所有额外的东西删掉了一些代码,但上面是两个基本文件。 They're both in the same folder and when I click the button I want to be able to go from first page to second page.它们都在同一个文件夹中,当我单击按钮时,我希望能够从第一页到第二页 go。 I feel like I'm dumb and the answer is really obvious but I just can't figure it out.我觉得自己很笨,答案很明显,但我就是想不通。

I think the best way to do this would be using Stack Navigation, like in this example of a project that I have:我认为最好的方法是使用堆栈导航,就像我拥有的这个项目示例一样:

the stack component:堆栈组件:

 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;

Where I click the button to navigate:我点击按钮导航的位置:

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

The app.tsx or app.js 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;

Edit: Check this expo snack: https://snack.expo.dev/@guilhermemoretto12/react-native-stack-navigator-example编辑:检查这个 expo 小吃: https://snack.expo.dev/@guilhermemoretto12/react-native-stack-navigator-example

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

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