react-native-navigation的迁移库

third-party-mobx.mdx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ---
  2. id: third-party-mobx
  3. title: MobX
  4. sidebar_label: MobX
  5. ---
  6. import useBaseUrl from '@docusaurus/useBaseUrl';
  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. :::info Note
  11. With the introduction of the new Context API, there is no need to use `Provider` pattern with MobX and you
  12. can now just use the React Context API.
  13. Also the example uses `mobx-react-lite` but you can use the normal `mobx-react`.
  14. :::
  15. ## Sharing a store between multiple screens
  16. In the example bellow we will be creating a small Counter app. We will learn how to integrate Mobx with React-Native-Navigation and demonstrate how updating the store from one component, triggers renders in other components connected to the same store.
  17. Once you finish implementing the example, your screen should look similar to this:
  18. <img width="30%" src={useBaseUrl('/img/mobx_counter.png')} />
  19. ### Step 1 - Create a Counter store
  20. Let's first create a counter store using MobX. Our store has a single `count` object and two methods to increment and decrement it.
  21. ```tsx
  22. // counter.store.js
  23. import React from 'react';
  24. import { observable, action } from 'mobx';
  25. class CounterStore {
  26. @observable count = 0;
  27. @action.bound
  28. increment() {
  29. this.count += 1;
  30. }
  31. @action.bound
  32. decrement() {
  33. this.count -= 1;
  34. }
  35. }
  36. // Instantiate the counter store.
  37. const counterStore = new CounterStore()
  38. // Create a React Context with the counter store instance.
  39. export const CounterStoreContext = React.createContext(counterStore)
  40. ```
  41. ### Step 2 - Consuming the store
  42. You can consume the Counter store in any React components using `React.useContext`.
  43. ```tsx
  44. // CounterScreen.js
  45. import React from 'react';
  46. import { Button, Text, View } from 'react-native';
  47. import { observer } from 'mobx-react-lite';
  48. import { CounterStoreContext } from './counter.store';
  49. const CounterScreen = observer((props) => {
  50. const { count, increment, decrement } = React.useContext(CounterStoreContext);
  51. return (
  52. <Root>
  53. <Text>{`Clicked ${count} times!`}</Text>
  54. <Button title='Increment' onPress={increment} />
  55. <Button title='Decrement' onPress={decrement} />
  56. <Button title='Push' onPress={() => Navigation.push(props.componentId, 'CounterScreen')}/>
  57. </Root>
  58. )
  59. });
  60. module.exports = CounterScreen;
  61. ```
  62. ## How to use MobX persistent data
  63. Often the app will require a persistent data solution and with MobX you can use [`mobx-react-persist`](https://github.com/pinqy520/mobx-persist).
  64. It only takes few extra steps to integrate the library.
  65. ```tsx
  66. //counter.store.js
  67. import React from 'react';
  68. import { observable, action } from 'mobx';
  69. import { persist } from 'mobx-persist'; // add this.
  70. class CounterStore {
  71. @persist @observable count = 0; // count is now persistent.
  72. @action.bound
  73. increment() {
  74. this.count += 1;
  75. }
  76. @action.bound
  77. decrement() {
  78. this.count -= 1;
  79. }
  80. }
  81. export const counterStore = new CounterStore(); // You need to export the counterStore instance.
  82. export const CounterStoreContext = React.createContext(counterStore);
  83. // index.js
  84. import { Navigation } from 'react-native-navigation';
  85. import AsyncStorage from '@react-native-community/async-storage';
  86. import { create } from 'mobx-persist';
  87. import { counterStore } from './counter.store'; // import the counter store instance.
  88. // Create a store hydration function.
  89. async function hydrateStores() {
  90. const hydrate = create({ storage: AsyncStorage });
  91. await hydrate('CounterStore', counterStore);
  92. }
  93. Navigation.events().registerAppLaunchedListener(() => {
  94. hydrateStores().then(() => {
  95. // ...register screens and start the app.
  96. });
  97. });
  98. ```