
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
babel-plugin-mobx-observer-on-every-react-component
Advanced tools
Wrap literally every React component in an MobX observer higher order component.
This plugin will wrap literally every React component in a MobX observer higher order component.
When using MobX, MobX-State-Tree, or any other library in the MobX ecosystem, you may find yourself in one of two scenarios:
observer
from mobx-react
or @observer
from mobx-react-lite
in every single file and wrap your components. This is something that seems to come up again and again.The typical recommendation from MobX is to just use the @observer
decorator or the observer
HOC and accept that you'll have to import it and use it in every file. This plugin doesn't actually change that requirement, it just makes it so that you don't have to manually do that. You may find value in this plugin if you either hate having to remember that, or if you just don't like the way it looks.
Not that we have evidence for. The MobX docs specifically recommend wrapping every component in an observer, saying:
observer only enhances the component you are decorating, not the components called by it. So usually all your components should be wrapped by observer. Don't worry, this is not inefficient. On the contrary, more observer components make rendering more efficient as updates become more fine-grained.
We don't actually have benchmarks to share, so it's always possible there's some small cost, but it's likely to be negligible. If you disagree, send us some benchmarks and we'll update this section!
# npm
npm install --save-dev babel-plugin-mobx-observer-on-every-react-component
# yarn
yarn add --dev babel-plugin-mobx-observer-on-every-react-component
# pnpm
pnpm add --dev babel-plugin-mobx-observer-on-every-react-component
# bun
bun add --dev babel-plugin-mobx-observer-on-every-react-component
Add the plugin to your .babelrc
file:
{
"plugins": [
["babel-plugin-mobx-observer-on-every-react-component"]
]
}
If you prefer using a JavaScript configuration file, you can add the plugin to your babel.config.js
:
module.exports = function(api) {
api.cache(true);
return {
plugins: [
'babel-plugin-mobx-observer-on-every-react-component'
]
};
};
You can pass an optional object to the plugin to configure the behavior of the plugin. Options look like this:
/**
* Options for the plugin
*/
interface PluginOptions {
// Default false, controls if we log debug statements during plugin execution. Mostly intended for plugin developers.
debugEnabled?: boolean;
}
Here are some options we'll eventually add:
If you want to ignore an entire file, add a comment to the top of the file:
// @auto-observer-ignore-file
If you want to ignore a specific block, add a comment to the start of the block:
// @auto-observer-ignore-block
cd example
bun install
bun dev
Then check out the Vite app that starts. It'll look something like this:
https://github.com/user-attachments/assets/1e1dc69e-defe-4387-82c9-c91ab0a5c887
You can see the plugin in action with different starting points using the Babel REPL. This is also a great way to provide us with reproducible examples for bug reports.
We'd love to have any help. Please open an issue or discussion if you want to get started.
Here are some resources we've used to learn how to develop Babel plugins:
We use Bun for package management, running tests, and building the project (sort of - we are actually using tsc under the hood with Bun as a task runner for this).
bun install
bun test
cd example && bun install
bun dev
to start the dev server and see a Vite app that points to a local build of this package.bun run build
in the root of the project and restart the dev server in the example folder.bun run build
in the root of the project.FAQs
Wrap literally every React component in an MobX observer higher order component.
We found that babel-plugin-mobx-observer-on-every-react-component demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.