Skip to content
This repository has been archived by the owner on Oct 11, 2020. It is now read-only.

Add section on babel macro to observer HOC doc #49

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions content/observe/hoc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,34 @@ export const Counter = observer<Props>(props => {
)
})
```

## Babel macro
BernieCr marked this conversation as resolved.
Show resolved Hide resolved

By default, components wrapped with the observer HOC are anonymous. The `Counter` component from the example above would only
show up as `wrappedComponent` or `Memo` in the React DevTools or in console log mesages.

For a better developer experience, there is a Babel macro included in the `mobx-react-lite` package, which
transforms components wrapped with `observer` to retain their name.

In order to use the macro, install [`babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros).

```
danielkcz marked this conversation as resolved.
Show resolved Hide resolved
npm install --save-dev babel-plugin-macros
```

Next add the `babel-plugin-macros` plugin to your Babel config (`.babelrc`):

```
{
"plugins": ["macros"]
}
```

Now you can use the macro simply by changing the import declarations from `mobx-react-lite` to `mobx-react-lite/macro`.

```tsx
import { observer, useLocalStore } from 'mobx-react-lite/macro'
```