Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

javascript - 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. I want to be able to go from one component to another when the button is clicked.

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

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. I feel like I'm dumb and the answer is really obvious but I just can't figure it out.

question from:https://stackoverflow.com/questions/65943409/navigate-between-different-components-react-native

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...