UNPKG

21.5 kBMarkdownView Raw
1# mobx-react
2
3[![CircleCI](https://circleci.com/gh/mobxjs/mobx-react.svg?style=svg)](https://circleci.com/gh/mobxjs/mobx-react)
4[![CDNJS](https://img.shields.io/cdnjs/v/mobx-react.svg)](https://cdnjs.com/libraries/mobx-react)
5[![Minzipped size](https://img.shields.io/bundlephobia/minzip/mobx-react-lite.svg)](https://bundlephobia.com/result?p=mobx-react-lite)
6
7[![TypeScript](https://badges.frapsoft.com/typescript/code/typescript.svg?v=101)](https://github.com/ellerbrock/typescript-badges/)[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
8
9[![Discuss on Github](https://img.shields.io/badge/discuss%20on-GitHub-orange)](https://github.com/mobxjs/mobx/discussions)
10[![View changelog](https://img.shields.io/badge/changelogs.xyz-Explore%20Changelog-brightgreen)](https://changelogs.xyz/mobx-react)
11
12Package with React component wrapper for combining React with MobX.
13Exports the `observer` decorator and other utilities.
14For documentation, see the [MobX](https://mobxjs.github.io/mobx) project.
15There is also work-in-progress [user guide](https://mobx-react.js.org) for additional information.
16This package supports both React and React Native.
17
18## Choosing your version
19
20There are currently two actively maintained versions of mobx-react:
21
22| NPM Version | Support MobX version | Supported React versions | Supports hook based components |
23| ----------- | -------------------- | ------------------------ | -------------------------------------------------------------------------------- |
24| v7 | 6.\* | 16.8+ | Yes |
25| v6 | 4._ / 5._ | 16.8+ | Yes |
26| v5 | 4._ / 5._ | 0.13+ | No, but it is possible to use `<Observer>` sections inside hook based components |
27
28mobx-react 6 / 7 is a repackage of the smaller [mobx-react-lite](https://github.com/mobxjs/mobx-react-lite) package + following features from the `mobx-react@5` package added:
29
30- Support for class based components for `observer` and `@observer`
31- `Provider / inject` to pass stores around (but consider to use `React.createContext` instead)
32- `PropTypes` to describe observable based property checkers (but consider to use TypeScript instead)
33- The `disposeOnUnmount` utility / decorator to easily clean up resources such as reactions created in your class based components.
34
35## Installation
36
37`npm install mobx-react --save`
38
39Or CDN: https://unpkg.com/mobx-react (UMD namespace: `mobxReact`)
40
41```javascript
42import { observer } from "mobx-react"
43```
44
45This package provides the bindings for MobX and React.
46See the [official documentation](https://mobx.js.org/react-integration.html) for how to get started.
47
48For greenfield projects you might want to consider to use [mobx-react-lite](https://github.com/mobxjs/mobx-react-lite), if you intend to only use function based components. `React.createContext` can be used to pass stores around.
49
50## API documentation
51
52Please check [mobx.js.org](https://mobx.js.org/) for the general documentation. The documentation below highlights some specifics.
53
54### `observer(component)`
55
56Function (and decorator) that converts a React component definition, React component class, or stand-alone render function, into a reactive component. A converted component will track which observables are used by its effective `render` and automatically re-render the component when one of these values changes.
57
58#### Functional Components
59
60`React.memo` is automatically applied to functional components provided to `observer`. `observer` does not accept a functional component already wrapped in `React.memo`, or an `observer`, in order to avoid consequences that might arise as a result of wrapping it twice.
61
62#### Class Components
63
64When using component classes, `this.props` and `this.state` will be made observables, so the component will react to all changes in props and state that are used by `render`.
65
66`shouldComponentUpdate` is not supported. As such, it is recommended that class components extend `React.PureComponent`. The `observer` will automatically patch non-pure class components with an internal implementation of `React.PureComponent` if necessary.
67
68See the [MobX](https://mobxjs.github.io/mobx/refguide/observer-component.html) documentation for more details.
69
70```javascript
71import { observer } from "mobx-react"
72
73// ---- ES6 syntax ----
74const TodoView = observer(
75 class TodoView extends React.Component {
76 render() {
77 return <div>{this.props.todo.title}</div>
78 }
79 }
80)
81
82// ---- ESNext syntax with decorator syntax enabled ----
83@observer
84class TodoView extends React.Component {
85 render() {
86 return <div>{this.props.todo.title}</div>
87 }
88}
89
90// ---- or just use function components: ----
91const TodoView = observer(({ todo }) => <div>{todo.title}</div>)
92```
93
94### `Observer`
95
96`Observer` is a React component, which applies `observer` to an anonymous region in your component.
97It takes as children a single, argumentless function which should return exactly one React component.
98The rendering in the function will be tracked and automatically re-rendered when needed.
99This can come in handy when needing to pass render function to external components (for example the React Native listview), or if you
100dislike the `observer` decorator / function.
101
102```javascript
103class App extends React.Component {
104 render() {
105 return (
106 <div>
107 {this.props.person.name}
108 <Observer>{() => <div>{this.props.person.name}</div>}</Observer>
109 </div>
110 )
111 }
112}
113
114const person = observable({ name: "John" })
115
116ReactDOM.render(<App person={person} />, document.body)
117person.name = "Mike" // will cause the Observer region to re-render
118```
119
120In case you are a fan of render props, you can use that instead of children. Be advised, that you cannot use both approaches at once, children have a precedence.
121Example
122
123```javascript
124class App extends React.Component {
125 render() {
126 return (
127 <div>
128 {this.props.person.name}
129 <Observer render={() => <div>{this.props.person.name}</div>} />
130 </div>
131 )
132 }
133}
134
135const person = observable({ name: "John" })
136
137ReactDOM.render(<App person={person} />, document.body)
138person.name = "Mike" // will cause the Observer region to re-render
139```
140
141### `useLocalObservable` hook
142
143[User guide](https://mobx-react.js.org/state-local)
144
145Local observable state can be introduced by using the `useLocalObservable` hook, that runs once to create an observable store. A quick example would be:
146
147```javascript
148import { useLocalObservable, Observer } from "mobx-react-lite"
149
150const Todo = () => {
151 const todo = useLocalObservable(() => ({
152 title: "Test",
153 done: true,
154 toggle() {
155 this.done = !this.done
156 }
157 }))
158
159 return (
160 <Observer>
161 {() => (
162 <h1 onClick={todo.toggle}>
163 {todo.title} {todo.done ? "[DONE]" : "[TODO]"}
164 </h1>
165 )}
166 </Observer>
167 )
168}
169```
170
171When using `useLocalObservable`, all properties of the returned object will be made observable automatically, getters will be turned into computed properties, and methods will be bound to the store and apply mobx transactions automatically. If new class instances are returned from the initializer, they will be kept as is.
172
173It is important to realize that the store is created only once! It is not possible to specify dependencies to force re-creation, _nor should you directly be referring to props for the initializer function_, as changes in those won't propagate.
174
175Instead, if your store needs to refer to props (or `useState` based local state), the `useLocalObservable` should be combined with the `useAsObservableSource` hook, see below.
176
177Note that in many cases it is possible to extract the initializer function to a function outside the component definition. Which makes it possible to test the store itself in a more straight-forward manner, and avoids creating the initializer closure on each re-render.
178
179_Note: using `useLocalObservable` is mostly beneficial for really complex local state, or to obtain more uniform code base. Note that using a local store might conflict with future React features like concurrent rendering._
180
181### Server Side Rendering with `enableStaticRendering`
182
183When using server side rendering, normal lifecycle hooks of React components are not fired, as the components are rendered only once.
184Since components are never unmounted, `observer` components would in this case leak memory when being rendered server side.
185To avoid leaking memory, call `enableStaticRendering(true)` when using server side rendering.
186
187```javascript
188import { enableStaticRendering } from "mobx-react"
189
190enableStaticRendering(true)
191```
192
193This makes sure the component won't try to react to any future data changes.
194
195### Which components should be marked with `observer`?
196
197The simple rule of thumb is: _all components that render observable data_.
198If you don't want to mark a component as observer, for example to reduce the dependencies of a generic component package, make sure you only pass it plain data.
199
200### Enabling decorators (optional)
201
202Decorators are currently a stage-2 ESNext feature. How to enable them is documented [here](https://github.com/mobxjs/mobx#enabling-decorators-optional).
203
204### Should I still use smart and dumb components?
205
206See this [thread](https://www.reddit.com/r/reactjs/comments/4vnxg5/free_eggheadio_course_learn_mobx_react_in_30/d61oh0l).
207TL;DR: the conceptual distinction makes a lot of sense when using MobX as well, but use `observer` on all components.
208
209### `PropTypes`
210
211MobX-react provides the following additional `PropTypes` which can be used to validate against MobX structures:
212
213- `observableArray`
214- `observableArrayOf(React.PropTypes.number)`
215- `observableMap`
216- `observableObject`
217- `arrayOrObservableArray`
218- `arrayOrObservableArrayOf(React.PropTypes.number)`
219- `objectOrObservableObject`
220
221Use `import { PropTypes } from "mobx-react"` to import them, then use for example `PropTypes.observableArray`
222
223### `Provider` and `inject`
224
225See also [the migration guide to React Hooks](https://mobx-react.js.org/recipes-migration).
226
227_Note: usually there is no need anymore to use `Provider` / `inject` in new code bases; most of its features are now covered by `React.createContext`._
228
229`Provider` is a component that can pass stores (or other stuff) using React's context mechanism to child components.
230This is useful if you have things that you don't want to pass through multiple layers of components explicitly.
231
232`inject` can be used to pick up those stores. It is a higher order component that takes a list of strings and makes those stores available to the wrapped component.
233
234Example (based on the official [context docs](https://facebook.github.io/react/docs/context.html#passing-info-automatically-through-a-tree)):
235
236```javascript
237@inject("color")
238@observer
239class Button extends React.Component {
240 render() {
241 return <button style={{ background: this.props.color }}>{this.props.children}</button>
242 }
243}
244
245class Message extends React.Component {
246 render() {
247 return (
248 <div>
249 {this.props.text} <Button>Delete</Button>
250 </div>
251 )
252 }
253}
254
255class MessageList extends React.Component {
256 render() {
257 const children = this.props.messages.map(message => <Message text={message.text} />)
258 return (
259 <Provider color="red">
260 <div>{children}</div>
261 </Provider>
262 )
263 }
264}
265```
266
267Notes:
268
269- It is possible to read the stores provided by `Provider` using `React.useContext`, by using the `MobXProviderContext` context that can be imported from `mobx-react`.
270- If a component asks for a store and receives a store via a property with the same name, the property takes precedence. Use this to your advantage when testing!
271- When using both `@inject` and `@observer`, make sure to apply them in the correct order: `observer` should be the inner decorator, `inject` the outer. There might be additional decorators in between.
272- The original component wrapped by `inject` is available as the `wrappedComponent` property of the created higher order component.
273
274#### "The set of provided stores has changed" error
275
276Values provided through `Provider` should be final. Make sure that if you put things in `context` that might change over time, that they are `@observable` or provide some other means to listen to changes, like callbacks. However, if your stores will change over time, like an observable value of another store, MobX will throw an error.
277This restriction exists mainly for legacy reasons. If you have a scenario where you need to modify the set of stores, please leave a comment about it in this issue https://github.com/mobxjs/mobx-react/issues/745. Or a preferred way is to [use React Context](https://mobx-react.js.org/recipes-context) directly which does not have this restriction.
278
279#### Inject as function
280
281The above example in ES5 would start like:
282
283```javascript
284var Button = inject("color")(
285 observer(
286 class Button extends Component {
287 /* ... etc ... */
288 }
289 )
290)
291```
292
293A functional stateless component would look like:
294
295```javascript
296var Button = inject("color")(
297 observer(({ color }) => {
298 /* ... etc ... */
299 })
300)
301```
302
303#### Customizing inject
304
305Instead of passing a list of store names, it is also possible to create a custom mapper function and pass it to inject.
306The mapper function receives all stores as argument, the properties with which the components are invoked and the context, and should produce a new set of properties,
307that are mapped into the original:
308
309`mapperFunction: (allStores, props, context) => additionalProps`
310
311Since version 4.0 the `mapperFunction` itself is tracked as well, so it is possible to do things like:
312
313```javascript
314const NameDisplayer = ({ name }) => <h1>{name}</h1>
315
316const UserNameDisplayer = inject(stores => ({
317 name: stores.userStore.name
318}))(NameDisplayer)
319
320const user = mobx.observable({
321 name: "Noa"
322})
323
324const App = () => (
325 <Provider userStore={user}>
326 <UserNameDisplayer />
327 </Provider>
328)
329
330ReactDOM.render(<App />, document.body)
331```
332
333_N.B. note that in this *specific* case neither `NameDisplayer` nor `UserNameDisplayer` needs to be decorated with `observer`, since the observable dereferencing is done in the mapper function_
334
335#### Using `PropTypes` and `defaultProps` and other static properties in combination with `inject`
336
337Inject wraps a new component around the component you pass into it.
338This means that assigning a static property to the resulting component, will be applied to the HoC, and not to the original component.
339So if you take the following example:
340
341```javascript
342const UserName = inject("userStore")(({ userStore, bold }) => someRendering())
343
344UserName.propTypes = {
345 bold: PropTypes.boolean.isRequired,
346 userStore: PropTypes.object.isRequired // will always fail
347}
348```
349
350The above propTypes are incorrect, `bold` needs to be provided by the caller of the `UserName` component and is checked by React.
351However, `userStore` does not need to be required! Although it is required for the original stateless function component, it is not
352required for the resulting inject component. After all, the whole point of that component is to provide that `userStore` itself.
353
354So if you want to make assertions on the data that is being injected (either stores or data resulting from a mapper function), the propTypes
355should be defined on the _wrapped_ component. Which is available through the static property `wrappedComponent` on the inject component:
356
357```javascript
358const UserName = inject("userStore")(({ userStore, bold }) => someRendering())
359
360UserName.propTypes = {
361 bold: PropTypes.boolean.isRequired // could be defined either here ...
362}
363
364UserName.wrappedComponent.propTypes = {
365 // ... or here
366 userStore: PropTypes.object.isRequired // correct
367}
368```
369
370The same principle applies to `defaultProps` and other static React properties.
371Note that it is not allowed to redefine `contextTypes` on `inject` components (but is possible to define it on `wrappedComponent`)
372
373Finally, mobx-react will automatically move non React related static properties from wrappedComponent to the inject component so that all static fields are
374actually available to the outside world without needing `.wrappedComponent`.
375
376#### Strongly typing inject
377
378##### With TypeScript
379
380`inject` also accepts a function (`(allStores, nextProps, nextContext) => additionalProps`) that can be used to pick all the desired stores from the available stores like this.
381The `additionalProps` will be merged into the original `nextProps` before being provided to the next component.
382
383```typescript
384import { IUserStore } from "myStore"
385
386@inject(allStores => ({
387 userStore: allStores.userStore as IUserStore
388}))
389class MyComponent extends React.Component<{ userStore?: IUserStore; otherProp: number }, {}> {
390 /* etc */
391}
392```
393
394Make sure to mark `userStore` as an optional property. It should not (necessarily) be passed in by parent components at all!
395
396Note: If you have strict null checking enabled, you could muffle the nullable type by using the `!` operator:
397
398```
399public render() {
400 const {a, b} = this.store!
401 // ...
402}
403```
404
405By [migrating to React Hooks](https://mobx-react.js.org/recipes-migration) you can avoid problems with TypeScript.
406
407#### Testing store injection
408
409It is allowed to pass any declared store in directly as a property as well. This makes it easy to set up individual component tests without a provider.
410
411So if you have in your app something like:
412
413```javascript
414<Provider profile={profile}>
415 <Person age={"30"} />
416</Provider>
417```
418
419In your test you can easily test the `Person` component by passing the necessary store as prop directly:
420
421```
422const profile = new Profile()
423const mountedComponent = mount(
424 <Person age={'30'} profile={profile} />
425)
426```
427
428Bear in mind that using shallow rendering won't provide any useful results when testing injected components; only the injector will be rendered.
429To test with shallow rendering, instantiate the `wrappedComponent` instead: `shallow(<Person.wrappedComponent />)`
430
431### disposeOnUnmount(componentInstance, propertyKey | function | function[])
432
433Function (and decorator) that makes sure a function (usually a disposer such as the ones returned by `reaction`, `autorun`, etc.) is automatically executed as part of the componentWillUnmount lifecycle event.
434
435```javascript
436import { disposeOnUnmount } from "mobx-react"
437
438class SomeComponent extends React.Component {
439 // decorator version
440 @disposeOnUnmount
441 someReactionDisposer = reaction(...)
442
443 // decorator version with arrays
444 @disposeOnUnmount
445 someReactionDisposers = [
446 reaction(...),
447 reaction(...)
448 ]
449
450
451 // function version over properties
452 someReactionDisposer = disposeOnUnmount(this, reaction(...))
453
454 // function version inside methods
455 componentDidMount() {
456 // single function
457 disposeOnUnmount(this, reaction(...))
458
459 // or function array
460 disposeOnUnmount(this, [
461 reaction(...),
462 reaction(...)
463 ])
464 }
465}
466```
467
468## DevTools
469
470`mobx-react@6` and higher are no longer compatible with the mobx-react-devtools.
471That is, the MobX react devtools will no longer show render timings or dependency trees of the component.
472The reason is that the standard React devtools are also capable of highlighting re-rendering components.
473And the dependency tree of a component can now be inspected by the standard devtools as well, as shown in the image below:
474
475![hooks.png](hooks.png)
476
477## FAQ
478
479**Should I use `observer` for each component?**
480
481You should use `observer` on every component that displays observable data.
482Even the small ones. `observer` allows components to render independently from their parent and in general this means that
483the more you use `observer`, the better the performance become.
484The overhead of `observer` itself is negligible.
485See also [Do child components need `@observer`?](https://github.com/mobxjs/mobx/issues/101)
486
487**I see React warnings about `forceUpdate` / `setState` from React**
488
489The following warning will appear if you trigger a re-rendering between instantiating and rendering a component:
490
491```
492
493Warning: forceUpdate(...): Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.`
494
495```
496
497-- or --
498
499```
500
501Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.
502
503```
504
505Usually this means that (another) component is trying to modify observables used by this components in their `constructor` or `getInitialState` methods.
506This violates the React Lifecycle, `componentWillMount` should be used instead if state needs to be modified before mounting.