Skip to content

Commit 01c3a68

Browse files
committed
React Navigation v5 compatibility is here with new version, new example and new logos
1 parent d2c2b32 commit 01c3a68

File tree

8 files changed

+121
-104
lines changed

8 files changed

+121
-104
lines changed

.circleci/config.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

README.md

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
<img alt="React Navigation Helpers" src="https://github.com/WrathChaos/react-navigation-helpers/blob/master/assets/logo.png" width="1050"/>
1+
<img alt="React Navigation Helpers" src="assets/logo.png" width="1050"/>
22

3-
Helpers for React Navigation.
3+
[![Battle Tested ✅](https://img.shields.io/badge/-Battle--Tested%20%E2%9C%85-03666e?style=for-the-badge)](https://github.com/WrathChaos/react-navigation-helpers)
4+
5+
[![Helpers for React Navigation](https://img.shields.io/badge/-Helpers%20for%20React%20Navigation-orange?style=for-the-badge)](https://github.com/WrathChaos/react-navigation-helpers)
46

57
[![npm version](https://img.shields.io/npm/v/react-navigation-helpers.svg?style=for-the-badge)](https://www.npmjs.com/package/react-navigation-helpers)
68
[![npm](https://img.shields.io/npm/dt/react-navigation-helpers.svg?style=for-the-badge)](https://www.npmjs.com/package/react-navigation-helpers)
@@ -20,96 +22,121 @@ npm i react-navigation-helpers
2022

2123
## Peer Dependencies
2224

23-
###### IMPORTANT! You need install them.
25+
<i><b>IMPORTANT!</b> You need install them.</i>
2426

25-
```
27+
```json
2628
"react": ">= 16.x.x",
27-
"react-navigation": ">= 3.x.x"
29+
"react-navigation": ">= 5.x.x"
2830
```
2931

32+
## React Navigation Versions
33+
34+
- Use **v0.2.0+** for ReactNavigation **v5**
35+
- Use **v0.1.0+** for ReactNavigation **v4**
36+
3037
## Usage
3138

3239
### Global Level Navigator
3340

34-
<b>Firstly</b> set the global level navigator reference
41+
Set the global level navigation reference into the `NavigationContainer`
3542

3643
```js
3744
import NavigationService from "react-navigation-helpers";
38-
39-
40-
<MainNavigator
41-
ref={navigatorRef =>
42-
NavigationService.setGlobalLevelNavigator(navigatorRef)
43-
}
44-
/>
45+
import { isReadyRef, navigationRef } from "react-navigation-helpers";
46+
47+
React.useEffect(() => {
48+
return () => (isReadyRef.current = false);
49+
}, []);
50+
51+
<NavigationContainer
52+
ref={navigationRef}
53+
onReady={() => {
54+
isReadyRef.current = true;
55+
}}
56+
>
57+
{/* ... */}
58+
</NavigationContainer>;
4559
```
4660

47-
### NavigationService Usage
61+
## NavigationService Usage
4862

49-
#### Navigate Example
63+
### Navigate Example
5064

5165
```js
52-
NavigationService.navigate("home")
66+
import * as NavigationService from "react-navigation-helpers";
67+
68+
NavigationService.navigate("home");
5369
```
5470

55-
#### Push Example
71+
### Push Example
5672

5773
```js
58-
NavigationService.push("home")
74+
import * as NavigationService from "react-navigation-helpers";
75+
76+
NavigationService.push("home");
5977
```
6078

61-
#### Pop Example
79+
### Pop Example
6280

6381
```js
64-
NavigationService.pop()
82+
import * as NavigationService from "react-navigation-helpers";
83+
84+
NavigationService.pop();
6585
```
6686

67-
#### PopToTop Example
87+
### PopToTop Example
6888

6989
```js
70-
NavigationService.popToTop()
90+
import * as NavigationService from "react-navigation-helpers";
91+
92+
NavigationService.popToTop();
7193
```
7294

73-
#### Back Example
95+
### Back Example
7496

7597
```js
76-
NavigationService.back()
98+
import * as NavigationService from "react-navigation-helpers";
99+
100+
NavigationService.back();
77101
```
78102

79103
## How to pass prop with this library?
80104

81105
The usage does not change. Simply put your prop as the secondary prop **as same as React Navigation** itself.
82106

83-
#### Navigate
107+
### Navigate
108+
84109
```js
85-
NavigationService.navigate("home", {data: myData, myId: "d1f01df1" })
110+
import * as NavigationService from "react-navigation-helpers";
111+
112+
NavigationService.navigate("home", { data: myData, myId: "d1f01df1" });
86113
```
87114

88-
#### Push
115+
### Push
116+
89117
```js
90-
NavigationService.push("home", {data: myData, myId: "d1f01df1" })
91-
```
118+
import * as NavigationService from "react-navigation-helpers";
92119

120+
NavigationService.push("home", { data: myData, myId: "d1f01df1" });
121+
```
93122

94123
## How to receive the passed props from navigation or push functions?
95124

96125
```js
97-
const data = props.navigation.getParam("data", null) // Second one is default value
98-
const myId = props.navigation.getParam("myId", "") // Second one is default value
126+
const data = props.navigation.getParam("data", null); // Second one is default value
127+
const myId = props.navigation.getParam("myId", ""); // Second one is default value
99128
```
100129

101130
### Configuration - Props
102131

103-
| Property | Type | Default | Description |
104-
| ----------------------- | :------: | :------: | -------------------------------------- |
105-
| setGlobalLevelNavigator | function | function | set your global level navigator (MUST) |
106-
| navigate | function | function | navigate the selected screen |
107-
| push | function | function | push the selected screen |
108-
| back | function | function | back the previous screen |
109-
| pop | function | function | pop the previous screen |
110-
| popToTop | function | function | pop the top level of the screen |
111-
| reset | function | function | reset the navigator |
112-
132+
| Property | Type | Default | Description |
133+
| -------- | :------: | :------: | ------------------------------- |
134+
| navigate | function | function | navigate the selected screen |
135+
| push | function | function | push the selected screen |
136+
| goBack | function | function | back the previous screen |
137+
| pop | function | function | pop the previous screen |
138+
| popToTop | function | function | pop the top level of the screen |
139+
| reset | function | function | reset the navigator |
113140

114141
### ToDos
115142

assets/icon.png

-2.91 KB
Binary file not shown.

assets/logo.png

4.47 KB
Loading

assets/splash.png

-7.01 KB
Binary file not shown.

lib/src/NavigationService.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as React from "react";
2+
import { StackActions } from "@react-navigation/native";
3+
4+
export const isReadyRef = React.createRef();
5+
export const navigationRef = React.createRef();
6+
7+
export const navigate = (routeName, params) => {
8+
if (isReadyRef.current && navigationRef.current) {
9+
// Perform navigation if the app has mounted
10+
navigationRef.current.navigate(routeName, params);
11+
}
12+
};
13+
14+
export const push = (routeName, params, ...args) => {
15+
if (isReadyRef.current && navigationRef.current) {
16+
// Perform navigation if the app has mounted
17+
navigationRef.current?.dispatch(
18+
StackActions.push(routeName, params, ...args)
19+
);
20+
}
21+
};
22+
23+
export const goBack = (...args) => {
24+
if (isReadyRef.current && navigationRef.current) {
25+
// Perform navigation if the app has mounted
26+
navigationRef.current.goBack(...args);
27+
}
28+
};
29+
30+
export const pop = (...args) => {
31+
if (isReadyRef.current && navigationRef.current) {
32+
// Perform navigation if the app has mounted
33+
navigationRef.current?.dispatch(StackActions.pop(...args));
34+
}
35+
};
36+
37+
export const popToTop = (...args) => {
38+
if (isReadyRef.current && navigationRef.current) {
39+
// Perform navigation if the app has mounted
40+
navigationRef.current?.dispatch(StackActions.popToTop(...args));
41+
}
42+
};
43+
44+
export const reset = (...args) => {
45+
if (isReadyRef.current && navigationRef.current) {
46+
// Perform navigation if the app has mounted
47+
navigationRef.current.reset(...args);
48+
}
49+
};

lib/src/index.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "react-navigation-helpers",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Helpers for React Navigation",
55
"keywords": [
6+
"v5",
67
"helpers",
78
"react-navigation",
89
"navigation",
@@ -18,12 +19,12 @@
1819
],
1920
"homepage": "https://www.freakycoder.com",
2021
"bugs": "https://github.com/WrathChaos/react-navigation-helpers/issues",
21-
"main": "./lib/src/index.js",
22+
"main": "./lib/src/NavigationService.js",
2223
"repository": "git@github.com:WrathChaos/react-navigation-helpers.git",
2324
"author": "Kuray (FreakyCoder) <kurayogun@gmail.com>",
2425
"license": "MIT",
2526
"peerDependencies": {
2627
"react": ">= 16.x.x",
27-
"react-navigation": ">= 3.x.x"
28+
"react-navigation": ">= 5.x.x"
2829
}
2930
}

0 commit comments

Comments
 (0)