
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
mkp-react-native-router
Advanced tools
This module is Navigator extension.you can manage all of route with configuration.

npm install mkp-react-native-router --save
IOS/Android
<Router ref="router"
renderTitle={(route)=> {
return <Text style={{color: "white"}}>{route.title}</Text>;
}}
renderLeftButton={(route, navigator, index)=> {
if (index > 0) {
return <Text style={{color: "white"}} onPress={event=> {
navigator.$pop();
}}>back</Text>
}
return null;
}}
navigationBarStyle={{backgroundColor: "black"}}
routes={[{
path: "home",
title: "Home",
component: Home
}, {
path: "register",
title: "Register-Step1",
component: RegisterStep1,
routes: [{
path: "step2",
title: "Register-Step2",
hideNavigationBar: true,
component: RegisterStep2
}]
}]}>
</Router>
Set title for navigation.
the default value will return a text node:
<Text>{route.title}</Text>
Set back button or left button for navigation, null as default value.
Set navigation style
The first route as initial route
configure page transition, you can refer to React Native Navigator
the default value is Navigator.SceneConfigs.HorizontalSwipeJump.
Invoke when navigator $push,$pop,$replace,$refreshNavBar
router will push to target path.the parameter route will override route which find by path.
//go to 'register'
this.props.navigator.$push("register")
//go to 'register/register-step2'
this.props.navigator.$push("register/register-step2");
//override route which find by path with the second parameter
this.props.navigator.$push("register",{
title:"Register"
});
in addition you can pass props through the second parameter.
this.props.navigator.$push("register",{
tel:"13100000000"
})
back to previous route.
replace current route with path. the second parameter is the same as $push
Refresh navigation's style.
this.props.navigator.$refreshNavBar({
title:"test",
renderLeftButton:()=>{},
renderRightButton:()=>{}
})
NOTE:this method must't be calling in component's lifecycle, such as componentDidMount,only calling in sceneDidFocussceneDidFocus.
Refer to Navigator.onDidFocus
class TestComponent extends Component{
sceneDidFocus(){
//do something
}
}
Refer to Navigator.onWillFocus
class TestComponent extends Component{
sceneWillFocus(){
//do something
}
}
const routes = [{
path: "home",
title: "Home",
component: Home
}, {
path: "register",
title: "Register-Step1",
component: RegisterStep1,
routes: [{
path: "step2",
title: "Register-Step2",
hideNavigationBar: true,
component: RegisterStep2
}]
},{
path: "login",
title: "Login",
component: Login
}, {
path: "mine",
title: "Mine",
component: Mine,
onEnter: ()=> {
if (!isLogin) {
return "login";
}
}
}];
after login success.
this.props.navigator.$replace(this.props.route.$previousPath);
or
this.props.navigator.$pop();
More time we determine login state with token , but we can't get token value in sync , so we need a page to initial some data , such as token . after we can use these data in sync. when you need to restore redux , the step is useful. sample code following:
window.appData={
token:""
};
class Init extends Component{
componentDidMount(){
getToken().then(value=>{
if(value){
window.appData.token=value;
this.props.navigator.$replace("home");
}
else{
this.props.navigator.$replace("login");
}
})
}
}
you can get current net state or anything data in here except above.
import Router from "mkp-react-native-router";
import {connect, Provider} from "react-redux";
const RouterWithRedux = connect()(Router);
<Provider store={store}>
<RouterWithRedux
navigationBarStyle={navigationStyles.navigationBar}
renderTitle={(route)=> {
return (
<View style={[navigationStyles.base]}>
<Text style={[navigationStyles.title]}>{route.title}</Text>
</View>
);
}}
renderLeftButton={(route, navigator, index)=> {
if (index > 0) {
return (
<TouchableHighlight
style={[navigationStyles.base, navigationStyles.leftButton]}
onPress={event=> {
navigator.$pop();
}}>
<Image source={require("./themes/assets/icons/back-icon.png")}/>
</TouchableHighlight >
);
}
return null;
}}
routes={routes}></RouterWithRedux>
</Provider>
you can deal with some action that is 'SCENE_WILL_FOCUS' and 'SCENE_DID_FOCUS' in reducer. example following,
import {ActionTypes} from "mkp-react-native-router";
export function testReducer(state,action){
switch(action.type){
case ActionTypes.SCENE_WILL_FOCUS:
//you can access the route from action.route
//do something
break;
case ActionTypes.SCENE_DID_FOCUS:
//you can access the route from action.route
//do something
break;
default:
return state;
}
}
Router provide two property currentRoute and navigator, you can do something through these property. following code:
class Test extends Component{
constructor(props){
super(props);
BackAndroid.addEventListener("hardwareBackPress",()=>{
// if router with redux
//let cur=this.refs.router.renderedElement._owner._renderedComponent._instance.currentRoute;
//let navigator=this.refs.router.renderedElement._owner._renderedComponent._instance.navigator;
//else
//let cur=this.refs.router.currentRoute;
//let navigator=this.refs.router.navigator;
let cur=this.refs.router.renderedElement._owner._renderedComponent._instance.currentRoute;
let navigator=this.refs.router.renderedElement._owner._renderedComponent._instance.navigator;
if(cur.path==="login" || cur.path==="home"){
//exit
confirm("确定要关闭应用吗?",ok=>{
if(ok){
BackAndroid.exitApp();
}
})
}
else{
navigator.$pop();
}
return true;
});
}
render(){
return (
<Router ref="router"></Router>
)
}
}
<Router
renderTitle={(route)=>{
return (
<View style={{flex:1,justifyContent:"center",alignItems:"center",...Platform.select({
android:{
width:Dimensions.get("window").width-72*2
}
})}}>
<Text>{route.title}</Text>
</View>
);
}}
/>
FAQs
react-native-router
The npm package mkp-react-native-router receives a total of 2 weekly downloads. As such, mkp-react-native-router popularity was classified as not popular.
We found that mkp-react-native-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.