A
A
Alexey Nikolaev2018-05-25 18:58:51
React
Alexey Nikolaev, 2018-05-25 18:58:51

Is it good practice to use connect really often?

Good day.
There are two cases.
1. Many components that have many sub-components that require access to the state. At the initial stage, I lowered state into props from the top connected components, but now I see it more convenient not to clog props with direct dependencies (the component will not work without state), but to make 80-90% of these sub-components connected, of course, without fanaticism.
2. There is a property, stored in state, which is used by half of the application. This is the role of the user in the system, based on it, the components decide which markup / component to give. Often the component only needs this role, so I pass it to props. But I don't see props as a good place for component dependencies , it's more of a place for parameters. You can use connect for the entire half of the application, but I came up with an alternative solution: write a function (or a class with methods) that would have access to the store itself. By calling this function anywhere in the application, the unconnected component will get the actual data from the state.
What do you think about each question? Is it generally good practice to use connect frequently, or is it better to keep several key components connected, and in smaller ones still use props, context, or the function described in the second paragraph?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Demian Smith, 2018-05-25
@Heian

In general, this whole story with smart / stupid components is about how to reuse these same components. The left hand rule is this: if the code depends on the external state, then such code becomes difficult to reuse. This applies not only to components, but to any code. connect adds a dependency on external state, so a connect-bound component is hard to use.
But it is quite normal to use connect on any components. And, if the connection makes it difficult to reuse a particular component, then you can always get rid of connect and pass props directly from the parent component. A kind of bottom-up development method.

D
davidnum95, 2018-05-30
@davidnum95

however, I also came up with an alternative solution: write a function (or a class with methods) that would have access to the store itself. By calling this function anywhere in the application, the unconnected component will get the actual data from the state.

Is that what you came up with hoc?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question