Automatically wrap all components with observer via a Babel plugin?
#3839
Replies: 2 comments 1 reply
-
|
It's possible with a caveat that you need a convention for indentifying a component function (eg first letter is uppercased, body contains JSX, ...). Personally I don't think it's worth it. It's recommended to use a named function instead of Our eslint plugin implements an autofix feature you may find useful: Also check: |
Beta Was this translation helpful? Give feedback.
-
|
I ended up writing such a plugin myself, turns out it wasn’t very difficult! It also removes existing /* ... */
plugins: [
["./tools/babel-plugin-auto-observe.js"],
],
/* ... */ |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm new to MobX and so far it seems very interesting, except that it feels a bit annoying having to wrap every single component with
observer, especially given that it breaks the name shown in the React Dev Tools when using arrow functions (I have seen the various workarounds at https://mobx.js.org/react-integration.html#displayname but neither of them seem particularly nice to me).But I was wondering, would it be possible to write a Babel plugin that automatically wraps every component defined in the code base with
observer, and also setsdisplayNameappropriately? It feels like it shouldn’t be too difficult to do. Or would that be a bad idea?I also saw #3798 which doesn’t require a Babel plugin, but I don’t think it solves the
displayNameproblem, and it also probably also deeply wraps components from external libraries, which is probably not a good idea.Example:
Beta Was this translation helpful? Give feedback.
All reactions