question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resolve injected properties after the props initialization

See original GitHub issue

What problem does this feature solve?

It’s said in the API reference that below the version 2.2.1 injected properties actually were resolved after the props initialization. So it seems like I’m asking for a regression.

I tried to find what formed the basis for making such a decision (to resolve them before props and data) but I haven’t been able to find any related issue.

I have a component <tree-view> that renders another component called <tree-item> inside. The tree-item may itself recursively render one or more tree-views.

The tree-view accepts a number of options that I pass to it as props (let it be a sole option called “mode” for the sake of brevity):

<tree-view
    mode="folders"
>
    ...
</tree-view>

The tree-view accepts it as a prop:

props: {
    mode: {
        default: "all",
        validator: ...
    }
}

And I want to provide it to all the nested tree-view’s, so that I don’t have to pass this option to the nested tree-item so that only from there it can be passed to the nested tree-view. But I can’t do so because the props since 2.2.1 are initialized after the injected properties.

What does the proposed API look like?

That is what I’m trying to achieve

props: {
    mode: {
        default: "all"
    }
},

provide() {
    return {
        /*
            This one works OK, this.mode is taken from the `props`
        */
        mode: this.mode
    }
},

inject: {
    mode: {
        /*
            But this one doesn't since the `props` are not yet initialized
        */
        default: this.mode
    }
}

My point is that the root tree-view isn’t provided with the mode injected property (because it’s the root one obviously) so the warning “Injection “mode” not found” is thrown for each root tree-view. But all the nested tree-view’s are provided with the injected property “mode” so the provided value is taken instead of the default one.

May be (I’m not sure, just a guess) the proposed API will need to like something like

inject() {
}

That’s that the injected property can accept a function as its value instead of just an array or an object.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
LinusBorgcommented, Feb 10, 2019
props: ['mode'],
inject: {
  injectedMode: {
    from: 'mode',
    default: null
  }
},
data() {
  return {
    mode: this.injectedMode || this.mode
  }
}

I posted this already, but in two pieces, essentially. Above you find the whole thing.

Default the inject to null, then construct a local data (or computed) from either her injected or, (if that’s null) the prop.

Result: no warnings, like you want to.

0reactions
smellyshovelcommented, Feb 10, 2019

@LinusBorg Yes, finally it works! Though there’s the error in your code: the prop must be of another name (bacause it can’t be named as the data property). So I changed the prop name to be propMode and it actually does finally work! Thank you!

UPD: The only drawback I see in your approach is that I now have to pass propMode="value" instead of mode="value" to the tree-view. However I believe I can fix that. Or even if not I can live with this.

Btw, have you considered my demo? I guess it’s much cleaner, except for the warning. Once again, props don’t warn if they’re not provided with any value. Why do so for injected? Maybe get rid of them?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Property and Method Injection — Autofac 6.0.0 documentation
Property injection uses writeable properties rather than constructor parameters to perform injection. Method injection sets dependencies by calling a method.
Read more >
Dependency injection · React in patterns - GitBook
In the constructor of that component we are resolving the dependencies and later while rendering the original component we pass them as props....
Read more >
Advanced Dependency Injection in React - Bits and Pieces
Dependency injection is a pattern that reduces hardcoded dependencies. It promotes composability by enabling one dependency to be replaced ...
Read more >
Can't get property dependency injection to work in React project
Using context or injection through props is not an option. I tried three solutions so far: Inversify · Inversify-React-Decorators · React.DI.
Read more >
When and Why you should do Dependency Injection in React
Dependency Injection is a popular pattern that is used to solve this ... initializing a service, and passing the reference as a props...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found