Skip to content
This repository was archived by the owner on Dec 31, 2020. It is now read-only.

Commit d1f7314

Browse files
ynejatiFredyCvkrol
authored
Issue #838: Clarify use of PureComponent and memo in changelog (#842)
* Issue #838: Clarify use of PureComponent and memo in changelog * Improve changelog and readme docs * Feedback. Lowercase observer in readme, better changelog link, shouldComponent update to lowercase as well. * Add 'the' before observer * Update README.md Co-Authored-By: Veniamin Krol <[email protected]> Co-authored-by: Daniel K. <[email protected]> Co-authored-by: Veniamin Krol <[email protected]>
1 parent b651658 commit d1f7314

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
**Improvements**
7575

7676
- Hook based components are now supported by mobx-react (in fact, the package is now implemented using hooks)
77-
- Using `PureComponent` is now _recommended_ for class-based components or `React.memo` for functional ones.
77+
- Class based `observer` components are now _recommended_ to extend `React.PureComponent`. Functional `observer` components are now automatically wrapped in `React.memo` internally. See section in [README](https://mobx.js.org/README.html#observercomponentclass) for more details.
7878
- For `observer` based components, there will now be an additional `Observer` component in the tree.
7979
- Two new hooks have been exposed, in case you want to manage local state in observable: `useLocalStore` and `useAsObservableSource`.
8080
- `MobXProviderContext` is now exposed from the package, in case you want to consume the context used by `Provider` with a `useContext` hook.

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,19 @@ For greenfield projects you might want to consider to use [mobx-react-lite](http
5151

5252
Please check [mobx.js.org](https://mobx.js.org) for the general documentation. The documentation below highlights some specifics.
5353

54-
### `observer(componentClass)`
54+
### `observer(component)`
5555

56-
Function (and decorator) that converts a React component definition, React component class or stand-alone render function into a reactive component, which tracks which observables are used by `render` and automatically re-renders the component when one of these values changes.
56+
Function (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
5763

5864
When 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`.
59-
Note that `observer` automatically applies `React.memo` to any functional component you pass to it, however class components should extend `PureComponent` instead of `Component`
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.
6067

6168
See the [MobX](https://mobxjs.github.io/mobx/refguide/observer-component.html) documentation for more details.
6269

src/observer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function observer<T extends IReactComponent>(component: T): T {
2727

2828
if (ReactMemoSymbol && component["$$typeof"] === ReactMemoSymbol) {
2929
throw new Error(
30-
"Mobx observer: You are trying to use 'observer' on function component wrapped to either another observer or 'React.memo'. The observer already applies 'React.memo' for you."
30+
"Mobx observer: You are trying to use 'observer' on a function component wrapped in either another observer or 'React.memo'. The observer already applies 'React.memo' for you."
3131
)
3232
}
3333

0 commit comments

Comments
 (0)