Answer the question
In order to leave comments, you need to log in
Should hoc be used if the same state is used in three places?
I have a list of university faculties on three pages, and accordingly in three different components in componentDidMount(). And then the Faculty field is created in the state, where the faculties are stored, which I later display in the form of selecta on these three pages. As I understand it, it's better to create a hoc in which I will receive these faculties, and transfer them from there to the components?
Answer the question
In order to leave comments, you need to log in
If Redux or another state-management library is used, it is better to put such data in the store.
If there are no such libraries, and the components are functional, you can write a hook.
If class-components, then it is possible to create HOC.
You can also create a Context, but since the pages are different, I would not use this option.
Look here. A class is a template for creating objects. The class state, that is, its fields, is unique for each instance. Regardless of the number of class instances, its methods will always work with its state, since they are context bound. In other words, the logic found in the methods is associated with the class object.
So hooks are an attempt to implement the behavior of methods in functions. That is, by defining a hook, you bind it to the life cycle of a functional component and its state. Simply put, for each copy of the functional component, a separate copy of the hook is created.
It follows from this that it is impossible to request data from the server in the hook, since each component using your hook will perform a separate request to the database. Hook is not a service. A hook is an attempt to implement the behavior of methods in functional components.
When different components need the same data, their receipt and storage is taken out on a separate corresponding layer (s). For React, this is redux/mobx.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question