Answer the question
In order to leave comments, you need to log in
When to use context and when to write your own hook in ReactJS?
I welcome everyone!
I'm learning react, and I had this question: use the context, and when to write your own hook?
For example, I got information about the current user via API, and I want to use this information in 10 different components (the example is just illustrative). There are roughly 2 options:
1) I can create a UserContext and extract information using the useContext hook in the desired component.
2) Also create a context and return data from this context through a self-written hook like useUser
Perhaps there is some third option that I don't know about.
Colleagues, I ask you, if possible, to give a detailed answer describing the reasons why you advise this way and not otherwise.
I am an experienced php developer and now I am trying to learn React. Due to many years of experience, I cannot accept any option simply because it was advised to me. I want to understand the reasons, pros and cons of every decision, otherwise my inner perfectionist will not let me rest
Answer the question
In order to leave comments, you need to log in
React does not imply any general architectural rules, everyone does what they want.
For example, I abstract into a separate hook any code that occurs more than twice and is longer than one line. Actually, exactly the same as with the selection of individual functions from the code. But that's me. And someone else does it differently.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question