@react-navigation/stack (ok)

https://reactnavigation.org/docs/navigating

import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
function HomeScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Home Screen</Text>
      <Button title="Go to Details" onPress={() => navigation.navigate('Details')} />
    </View>
  );
}
function DetailsScreen({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Details Screen</Text>
      <Button title="Go to Details... again" onPress={() => navigation.navigate('Details')} />
    </View>
  );
}
const Stack = createStackNavigator();
function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="Home">
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Details" component={DetailsScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}
export default App;

Ví dụ đã thành công :)

// C:\Users\Administrator\Desktop\App\App.js
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import Home from './components/Home';
import Blog from './components/Blog';
import BlogDetails from './components/BlogDetails';
const Stack = createStackNavigator();
function NavStack() {
  return (
    <Stack.Navigator initialRouteName="Home" screenOptions={{ headerTitleAlign: 'center', headerStyle: { backgroundColor: '#621FF7', }, headerTintColor: '#fff', headerTitleStyle :{ fontWeight: 'bold', }, }} >
      <Stack.Screen name="Home" component={Home} options={{ title: 'Home' }} /> 
      <Stack.Screen name="Blog" component={Blog} options={{ title: 'Blog' }} /> 
      <Stack.Screen name="BlogDetails" component={BlogDetails} options={{ title: 'Blog Detail' }} />
    </Stack.Navigator>
  );
}
function App() {
  return (
    <NavigationContainer>
      <NavStack />
    </NavigationContainer>
  );
}
export default App;
// C:\Users\Administrator\Desktop\App\components\Home.js
import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';
class Home extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Home Screen</Text>
        <Button title="Go to Blog" onPress={() => this.props.navigation.navigate('Blog')} />
        <Button title="Go to Blog Details" onPress={() => this.props.navigation.navigate('BlogDetails')} />
      </View>
    );
  }
}
export default Home;
//C:\Users\Administrator\Desktop\App\components\Blog.js 
import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';
class Blog extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Blog Screen</Text>
        <Button title="Go to Home" onPress={() => this.props.navigation.navigate('Home')} /> 
        <Button title="Go to Blog Details" onPress={() => this.props.navigation.navigate('BlogDetails')} />
      </View>
    );
  }
}
export default Blog;
// C:\Users\Administrator\Desktop\App\components\BlogDetails.js
import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';
class BlogDetails extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Blog Details Screen</Text>
        <Button title="Go to Home" onPress={() => this.props.navigation.navigate('Home')} /> 
        <Button title="Go to Blog" onPress={() => this.props.navigation.navigate('Blog')} />
      </View>
    );
  }
}
export default BlogDetails;

Last updated

Was this helpful?