Dependency injection and replacement for React components and hooks
This new version of react-magnetic-di
comes with tons of goodies!
di()
needed to be called in every function to enable injection and, moreover, it needed to contain all the values allowed for injection. It was a safe strategy but it was suboptimal. This version of magnetic-di
enables injection without di()
runWithDi
which enables replacement in any contextinjectable(A,B, { target: C })
you can replace a dependency only in a particular function scope.no-restricted-injectable
. Allows to warn/error about dangerous injectable patterns (eg on very commonly used dependencies)babel-plugin-macro
support, as di()
is now implicitexhaustive-inject
ESLint rule as no longer neededThe most substantial change is the different Babel plugin configuration. To facilitate the migration, you can maintain the previous behaviour by excluding all files from auto injection:
// In your .babelrc / babel.config.js
// ... other stuff like presets
plugins: [
// ...
['react-magnetic-di/babel-plugin', { exclude: [/.*/], enabledEnvs: ['development', 'test'] }],
],
Then, you can start excluding less from auto injection, only leaving test and mock files (we recommend it, to reduce chances of loops due to injectables injecting themselves). In case you hit an issue with auto di, you can opt out in a specific function by adding the magic comment // di-ignore
Fix an issue with stats
and the undocumented missing()
tracking functionality.
So far, we forgot to enforce that values passed in as injectables were actual injectables. So DiProvider
would accept anything and not flag that the provided value will be ignored. This releases fixes that. It will complain/break some tests but those "injectables" were not working anyway.
A side effect is a breaking change on one TS type: if you were using Dependency
before in your project, you'll likely have to replace it with Injectable
, as the latter is more strict.
di
injection under react-test-renderer