react.di icon indicating copy to clipboard operation
react.di copied to clipboard

Injection not working with React 16.8.6

Open fljmayer opened this issue 6 years ago • 11 comments

I am on React 16.8.6 and TypeScript 3.4.3, and I followed the instructions as much as possible. My project is just a small proof-of-concept with a single @Injectable service, a single @Injected attribute (see below) and a simple @Module({ providers: [ContentService] }) on the main application component. One exception is that I had to allow for the injected attribute to be uninitialized: @Inject contentService!: ContentService;

However, when componentDidMount() tries to call a method on contentService, the attribute is undefined.

fljmayer avatar May 23 '19 14:05 fljmayer

@fljmayer Can you provide an example repo? I cannot reproduce your issue?

RobinBuschmann avatar May 23 '19 17:05 RobinBuschmann

Here is the repo: https://github.com/fljmayer/react-pilot. Just run npm start and you should see the error in your browser.

fljmayer avatar May 24 '19 12:05 fljmayer

I couldn't get it to work neither. I'll give it another shot today.

RobinBuschmann avatar Jun 02 '19 08:06 RobinBuschmann

you need use babel metadata plugin

s97712 avatar Jun 22 '19 15:06 s97712

Hi s97712, thanks for your response. Could please be more specific about which Babel plugin I need to use? I am looking for the exact module name to be used with npm install --save-dev ....

fljmayer avatar Jun 25 '19 18:06 fljmayer

I'm use react-app-rewired for rewrite babel config, and here is my react-app-rewired config.

// file: ./config-overrides.ts
const {
    override,
    addBabelPlugins
} = require("customize-cra");

module.exports = override(
    addBabelPlugins(
        "babel-plugin-transform-typescript-metadata",
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        ["@babel/plugin-proposal-class-properties", { "loose": true }],
    )
)

s97712 avatar Jun 26 '19 09:06 s97712

I ejected the react-app and added the mentioned Babel plugins into package.json. It didn't help.

fljmayer avatar Jul 01 '19 21:07 fljmayer

Any update on this?

csisy avatar Jul 23 '19 15:07 csisy

+1

ashrpc avatar Sep 28 '19 19:09 ashrpc

Update: not react.di issue, problem inside webpack/babel config. In my case adding ts-loader to webpack loaders along with babel-loader solved issue.

ashrpc avatar Sep 29 '19 18:09 ashrpc

Sorry for posting to this old thread, but does it mean react.di won't work with unejected create-react-app?

glebsts avatar Feb 08 '20 21:02 glebsts