A
A
alex4answ2020-12-04 10:16:57
React
alex4answ, 2020-12-04 10:16:57

How to implement dependency injection in React?

There is a service for working with the API.

How to correctly insert dependencies into components on hooks?
If they were class-based, I would use TypeDI and insert them into the constructor.

But I have not seen anywhere that they use DI for react in general, all the examples that I find are just import and use of the service, how can I test it later, if necessary, replace it, etc.?

Can you please tell me how is it usually done?
Perhaps I'm overcomplicating the task and it's more correct to refuse services, di and so on, and sculpt like in most applications just use fetch directly in useEffect/didMount ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Lewandowski, 2020-12-04
@alex4answ

1. InversifyJS ;
2. You make a hook to get the service from the container. Here is an example .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question