react-native-navigation的迁移库

third-party-mobx.mdx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ---
  2. id: third-party-mobx
  3. title: MobX
  4. sidebar_label: MobX
  5. ---
  6. ## MobX
  7. MobX is one of the most popular state management libraries used by applications sized from small to large.
  8. With the introduction of the new React Context API, MobX can now be very easily integrated in React Native Navigation
  9. projects.
  10. In the example we will be creating a small Counter app.
  11. :::info Note
  12. With the introduction of the new Context API, there is no need to use `Provider` pattern with MobX and you
  13. can now just use the React Context API.
  14. Also the example uses `mobx-react-lite` but you can use the normal `mobx-react`.
  15. :::
  16. ## Create a Counter store.
  17. Let's first create a counter store using MobX.
  18. ```tsx
  19. // counter.store.js
  20. import React from 'react'
  21. import { observable, action } from 'mobx'
  22. class CounterStore {
  23. @observable count = 0
  24. @action.bound
  25. increment() {
  26. this.count += 1
  27. }
  28. @action.bound
  29. decrement() {
  30. this.count -= 1
  31. }
  32. }
  33. // Instantiate the counter store.
  34. const counterStore = new CounterStore()
  35. // Create a React Context with the counter store instance.
  36. export const CounterStoreContext = React.createContext(counterStore)
  37. ```
  38. ## Consuming the store
  39. You can consume the Counter store in any React components using `React.useContext`.
  40. ```tsx
  41. // Counter.js
  42. import React from 'react'
  43. import { Button, Text, View } from 'react-native'
  44. import { observer } from 'mobx-react-lite'
  45. import { CounterStoreContext } from './counter.store'
  46. const CounterScreen = observer(() => {
  47. const { count, increment, decrement } = React.useContext(CounterStoreContext)
  48. return (
  49. <View>
  50. <Text>{`Clicked ${count} times!`}</Text>
  51. <Button title="Increment" onPress={increment} />
  52. <Button title="Decrement" onPress={decrement} />
  53. </View>
  54. )
  55. })
  56. ```
  57. ## How to use MobX persistent data
  58. Often the app will require a persistent data solution and with MobX you can use [`mobx-react-persist`](https://github.com/pinqy520/mobx-persist).
  59. It only takes few extra steps to integrate the library.
  60. ```tsx
  61. //counter.store.js
  62. import React from 'react'
  63. import { observable, action } from 'mobx'
  64. import { persist } from 'mobx-persist' // add this.
  65. class CounterStore {
  66. @persist @observable count = 0 // count is now persistent.
  67. @action.bound
  68. increment() {
  69. this.count += 1
  70. }
  71. @action.bound
  72. decrement() {
  73. this.count -= 1
  74. }
  75. }
  76. export const counterStore = new CounterStore() // You need to export the counterStore instance.
  77. export const CounterStoreContext = React.createContext(counterStore)
  78. // index.js
  79. import { Navigation } from 'react-native-navigation
  80. import AsyncStorage from '@react-native-community/async-storage'
  81. import { create } from 'mobx-persist'
  82. import { counterStore } from './counter.store // import the counter store instance.
  83. // Create a store hydration function.
  84. async function hydrateStores() {
  85. const hydrate = create({ storage: AsyncStorage })
  86. await hydrate('CounterStore', counterStore)
  87. }
  88. Navigation.events().registerAppLaunchedListener(() => {
  89. hydrateStores().then(() => {
  90. // ...register screens and start the app.
  91. })
  92. })
  93. ```