|
@@ -0,0 +1,117 @@
|
|
1
|
+---
|
|
2
|
+id: third-party-react-context
|
|
3
|
+title: React Context API
|
|
4
|
+sidebar_label: React Context API
|
|
5
|
+---
|
|
6
|
+
|
|
7
|
+## React Context API
|
|
8
|
+React Context API provides a easy way to pass data through the component tree without having to pass props down
|
|
9
|
+manually at every level. You can find more about the Context API in [React documentation](https://reactjs.org/docs/context.html).
|
|
10
|
+
|
|
11
|
+You can use the React Context API with React Native Navigation with a limitation. In this example, we are going to create a screen which uses the Counter Context.
|
|
12
|
+
|
|
13
|
+:::important Limitation
|
|
14
|
+As RNN screens are not part of the same component tree, updating the values in the shared context does not trigger a re-render across all screens.
|
|
15
|
+However you can still use the React.Context per RNN screen component tree.
|
|
16
|
+
|
|
17
|
+If you need to trigger a re-render across all screens, there are many popular third party libraries such as
|
|
18
|
+[MobX](third-party-mobx.mdx) or Redux.
|
|
19
|
+:::
|
|
20
|
+
|
|
21
|
+## Create a Counter context
|
|
22
|
+
|
|
23
|
+```tsx
|
|
24
|
+// CounterContext.js
|
|
25
|
+import React from 'react
|
|
26
|
+
|
|
27
|
+// Declaring the state object globally.
|
|
28
|
+const initialCounterState = {
|
|
29
|
+ count: 0
|
|
30
|
+}
|
|
31
|
+
|
|
32
|
+const counterContextWrapper = (component) => ({
|
|
33
|
+ ...initialCounterState,
|
|
34
|
+ increment: () => {
|
|
35
|
+ initialCounterState.count += 1
|
|
36
|
+ component.setState({ context: contextWrapper(component) })
|
|
37
|
+ },
|
|
38
|
+ decrement: () => {
|
|
39
|
+ initialCounterState.count -= 1
|
|
40
|
+ component.setState({ context: contextWrapper(component) })
|
|
41
|
+ },
|
|
42
|
+})
|
|
43
|
+
|
|
44
|
+export const CounterContext = React.createContext({})
|
|
45
|
+
|
|
46
|
+export class CounterContextProvider extends React.Component {
|
|
47
|
+ state = {
|
|
48
|
+ context: counterContextWrapper(this)
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ render() {
|
|
52
|
+ return (
|
|
53
|
+ <CounterContext.Provider value={this.state.context}>
|
|
54
|
+ {this.props.children}
|
|
55
|
+ </CounterContext.Provider>
|
|
56
|
+ )
|
|
57
|
+ }
|
|
58
|
+}
|
|
59
|
+```
|
|
60
|
+
|
|
61
|
+## Register the screen
|
|
62
|
+When registering the screen that will be using the Counter Context, we need to wrap it
|
|
63
|
+with the Counter Context Provider we created earlier.
|
|
64
|
+
|
|
65
|
+```tsx
|
|
66
|
+// index.js
|
|
67
|
+import { Navigation } from 'react-native-navigation
|
|
68
|
+import { CounterContextProvider } from './CounterContext
|
|
69
|
+import { App } from './App
|
|
70
|
+
|
|
71
|
+Navigation.registerComponent(
|
|
72
|
+ 'App',
|
|
73
|
+ () => props => (
|
|
74
|
+ <CounterContextProvider>
|
|
75
|
+ <App {...props} />
|
|
76
|
+ </CounterContextProvider>
|
|
77
|
+ ),
|
|
78
|
+ () => App
|
|
79
|
+)
|
|
80
|
+```
|
|
81
|
+
|
|
82
|
+## Consuming the context
|
|
83
|
+You can consume the Counter Context any way you want such as `Provider.Consumer` or `React.useContext` like in the
|
|
84
|
+example below.
|
|
85
|
+
|
|
86
|
+```tsx
|
|
87
|
+// App.js
|
|
88
|
+import React from 'react'
|
|
89
|
+import { Button, Text, View } from 'react-native'
|
|
90
|
+import { CounterContext } from './CounterContext'
|
|
91
|
+
|
|
92
|
+export const App = () => {
|
|
93
|
+ // Using useContext API
|
|
94
|
+ const { count, increment, decrement } = React.useContext(CounterContext)
|
|
95
|
+
|
|
96
|
+ return (
|
|
97
|
+ <View>
|
|
98
|
+ <Text>{`Clicked ${count} times!`}</Text>
|
|
99
|
+ <Button title="Increment" onPress={increment} />
|
|
100
|
+ <Button title="Decrement" onPress={decrement} />
|
|
101
|
+ </View>
|
|
102
|
+ )
|
|
103
|
+
|
|
104
|
+ // Using Context consumer
|
|
105
|
+ return (
|
|
106
|
+ <CounterContext.Consumer>
|
|
107
|
+ {({ count, increment, decrement }) => (
|
|
108
|
+ <View>
|
|
109
|
+ <Text>{`Clicked ${count} times!`}</Text>
|
|
110
|
+ <Button title="Increment" onPress={increment} />
|
|
111
|
+ <Button title="Decrement" onPress={decrement} />
|
|
112
|
+ </View>
|
|
113
|
+ )}
|
|
114
|
+ </CounterContext.Consumer>
|
|
115
|
+ )
|
|
116
|
+}
|
|
117
|
+```
|