Skip to content

Commit 710d53c

Browse files
authored
using navigation ref to navigate without the navigation prop (#136)
* using navigation ref to navigate without the navigation prop * redux flipper inline require and use push instead of contact * navigate without navigation prop version
1 parent ea47456 commit 710d53c

File tree

9 files changed

+61
-32
lines changed

9 files changed

+61
-32
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@thecodingmachine/react-native-boilerplate",
3-
"version": "2.1.3",
3+
"version": "2.1.4",
44
"description": "TheCodingMachine React Native Boilerplate",
55
"repository": {
66
"type": "git",

Diff for: template/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "boilerplate",
3-
"version": "2.0.0",
3+
"version": "2.1.4",
44
"private": true,
55
"scripts": {
66
"android": "react-native run-android",
@@ -29,7 +29,7 @@
2929
"react-native-screens": "^2.14.0",
3030
"react-redux": "^7.2.1",
3131
"redux": "^4.0.5",
32-
"redux-flipper": "^1.4.0",
32+
"redux-flipper": "^1.4.2",
3333
"redux-persist": "^6.0.0"
3434
},
3535
"devDependencies": {

Diff for: template/src/App.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { store, persistor } from '@/Store'
66
import { SafeAreaView, StatusBar } from 'react-native'
77
import { NavigationContainer } from '@react-navigation/native'
88
import { ApplicationNavigator } from '@/Navigators'
9+
import { navigationRef } from '@/Navigators/Root'
910
import { Layout } from '@/Theme'
1011
import './Translations'
1112

@@ -20,7 +21,7 @@ const App = () => (
2021
*/}
2122
<PersistGate loading={null} persistor={persistor}>
2223
<SafeAreaView style={Layout.fill}>
23-
<NavigationContainer>
24+
<NavigationContainer ref={navigationRef}>
2425
<StatusBar barStyle="dark-content" />
2526
<ApplicationNavigator />
2627
</NavigationContainer>

Diff for: template/src/Containers/Startup/Index.js

+8-24
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,25 @@
11
import React, { useEffect } from 'react'
2-
import { ActivityIndicator, View, Button, Text } from 'react-native'
3-
import { Layout, Fonts } from '@/Theme'
4-
import { useDispatch, useSelector } from 'react-redux'
2+
import { ActivityIndicator, View, Text } from 'react-native'
3+
import { Layout, Fonts, Gutters } from '@/Theme'
4+
import { useDispatch } from 'react-redux'
55
import InitStartup from '@/Store/Startup/Init'
6-
import { CommonActions } from '@react-navigation/native'
76
import { useTranslation } from 'react-i18next'
7+
import { Brand } from '@/Components'
88

99
const IndexStartupContainer = ({ navigation }) => {
1010
const { t } = useTranslation()
1111

1212
const dispatch = useDispatch()
13-
const isApplicationLoading = useSelector((state) => state.startup.loading)
1413

1514
useEffect(() => {
1615
dispatch(InitStartup.action())
1716
}, [dispatch])
1817

1918
return (
20-
<View style={[Layout.fill, Layout.rowCenter]}>
21-
{isApplicationLoading ? (
22-
<ActivityIndicator />
23-
) : (
24-
<View>
25-
<Text style={Fonts.textCenter}>{t('welcome')}</Text>
26-
<Button
27-
title={t('actions.continue')}
28-
onPress={() =>
29-
navigation.dispatch(
30-
CommonActions.reset({
31-
index: 0,
32-
routes: [{ name: 'Main' }],
33-
}),
34-
)
35-
}
36-
/>
37-
</View>
38-
)}
19+
<View style={[Layout.fill, Layout.colCenter]}>
20+
<Brand />
21+
<ActivityIndicator size={'large'} style={[Gutters.largeVMargin]} />
22+
<Text style={Fonts.textCenter}>{t('welcome')}</Text>
3923
</View>
4024
)
4125
}

Diff for: template/src/Navigators/Application.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ const ApplicationNavigator = () => {
2424
<Stack.Navigator headerMode={'none'}>
2525
<Stack.Screen name="Startup" component={IndexStartupContainer} />
2626
{isApplicationLoaded && (
27-
<Stack.Screen name="Main" component={MainNavigator} />
27+
<Stack.Screen
28+
name="Main"
29+
component={MainNavigator}
30+
options={{
31+
animationEnabled: false,
32+
}}
33+
/>
2834
)}
2935
</Stack.Navigator>
3036
)

Diff for: template/src/Navigators/Root.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Used to navigating without the navigation prop
3+
* @see https://reactnavigation.org/docs/navigating-without-navigation-prop/
4+
*
5+
* You can add other navigation functions that you need and export them
6+
*/
7+
import * as React from 'react'
8+
import { CommonActions } from '@react-navigation/native'
9+
10+
export const navigationRef = React.createRef()
11+
12+
export function navigate(name, params) {
13+
navigationRef.current?.navigate(name, params)
14+
}
15+
16+
export function navigateAndReset(routes = [], index = 0) {
17+
navigationRef.current?.dispatch(
18+
CommonActions.reset({
19+
index,
20+
routes,
21+
}),
22+
)
23+
}
24+
25+
export function navigateAndSimpleReset(name, index = 0) {
26+
navigationRef.current?.dispatch(
27+
CommonActions.reset({
28+
index,
29+
routes: [{ name }],
30+
}),
31+
)
32+
}

Diff for: template/src/Store/Startup/Init.js

+6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import {
44
buildAsyncReducers,
55
} from '@thecodingmachine/redux-toolkit-wrapper'
66
import FetchOne from '@/Store/User/FetchOne'
7+
import { navigateAndSimpleReset } from '@/Navigators/Root'
78

89
export default {
910
initialState: buildAsyncState(),
1011
action: buildAsyncActions('startup/init', async (args, { dispatch }) => {
12+
// Timeout to fake waiting some process
13+
// Remove it, or keep it if you want display a beautiful splash screen ;)
14+
await new Promise((resolve) => setTimeout(resolve, 1000))
1115
// Here we load the user 1 for example, but you can for example load the connected user
1216
await dispatch(FetchOne.action(1))
17+
// Navigate and reset to the main navigator
18+
navigateAndSimpleReset('Main')
1319
}),
1420
reducers: buildAsyncReducers({ itemKey: null }), // We do not want to modify some item by default
1521
}

Diff for: template/src/Store/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
REGISTER,
1212
} from 'redux-persist'
1313
import { configureStore } from '@reduxjs/toolkit'
14-
import createDebugger from 'redux-flipper'
1514

1615
import startup from './Startup'
1716
import user from './User'
@@ -39,7 +38,8 @@ const store = configureStore({
3938
})
4039

4140
if (__DEV__ && !process.env.JEST_WORKER_ID) {
42-
middlewares.concat(createDebugger())
41+
const createDebugger = require('redux-flipper').default
42+
middlewares.push(createDebugger())
4343
}
4444

4545
return middlewares

Diff for: template/yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -6165,7 +6165,7 @@ readable-stream@^2.0.1, readable-stream@^2.2.2, readable-stream@~2.3.6:
61656165
string_decoder "~1.1.1"
61666166
util-deprecate "~1.0.1"
61676167

6168-
redux-flipper@^1.4.0:
6168+
redux-flipper@^1.4.2:
61696169
version "1.4.2"
61706170
resolved "https://registry.yarnpkg.com/redux-flipper/-/redux-flipper-1.4.2.tgz#da765a675136e9893a002730703ec58d2f0e359b"
61716171
integrity sha512-VfSdmWATaZD2BHAiMOEzxk+KSiSTAv/N5AyY64+dU+qif/KfinSHWW3+YVMCO+5rxne1Fr92YwsYJl8l9injDA==

0 commit comments

Comments
 (0)