📜  react-native-router-flux - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:46:59.712000             🧑  作者: Mango

React Native Router Flux

React Native Router Flux is a routing library that allows developers to easily navigate their application using declarative routing. It has a simple, easy-to-use API with a lot of features, such as navigating between screens, passing props, managing state and more.

Installation

To install React Native Router Flux, use the following command:

npm install --save react-native-router-flux
Basic Usage

Here is a basic example of how to use React Native Router Flux:

import React from 'react';
import { Router, Scene } from 'react-native-router-flux';
import Home from './Home';
import Profile from './Profile';
import Settings from './Settings';

const App = () => (
  <Router>
    <Scene key="root">
      <Scene key="home" component={Home} title="Home" initial/>
      <Scene key="profile" component={Profile} title="Profile"/>
      <Scene key="settings" component={Settings} title="Settings"/>
    </Scene>
  </Router>
);

export default App;

In this example, we define a Router component and define our scenes inside it. Each scene is defined with a key, a component, and a title. The initial property is used to define which scene is the initial one.

Navigation

React Native Router Flux provides a lot of ways to navigate between scenes. Here are some of the most common ones:

  • Actions.sceneKey: This method is used to navigate to a specific scene by its key.
import { Actions } from 'react-native-router-flux';

Actions.myScene();
  • Actions.pop(): This method is used to pop the current scene off the stack and go back to the previous one.
import { Actions } from 'react-native-router-flux';

Actions.pop();
  • Actions.reset(): This method is used to reset the stack and start from a specific scene.
import { Actions } from 'react-native-router-flux';

Actions.reset({ key: 'home' });
Passing Props

React Native Router Flux allows passing props between scenes. Here is how to pass props when navigating to a new scene:

import { Actions } from 'react-native-router-flux';

Actions.myScene({ prop1: value1, prop2: value2 });

And here is how to access the passed props inside the new scene:

const MyScene = (props) => (
  <View>
    <Text>{props.prop1}</Text>
    <Text>{props.prop2}</Text>
  </View>
);
Conclusion

React Native Router Flux is a powerful and easy-to-use routing library for React Native. It provides a lot of features for navigating your application and managing your state. Give it a try and see how it can improve your development experience!