Skip to content

Context best-practices #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

Conversation

gomorizsolt
Copy link
Collaborator

@gomorizsolt gomorizsolt commented Dec 22, 2019

Looking for ideas..

  • low-level examples if possible
  • rephrase sentences if necessary

@gomorizsolt gomorizsolt changed the title Contexts best-practices Context best-practices Dec 22, 2019
@thisismydesign
Copy link
Member

thisismydesign commented Dec 23, 2019

Found these articles you mentioned the last time quite useful. Probably worth a mention in the guides as well:

@gomorizsolt
Copy link
Collaborator Author

gomorizsolt commented Dec 24, 2019

Found these articles you mentioned the last time quite useful. Probably worth a mention in the guides as well:

Sounds good.

Brainstorm time. :)

I was thinking of these "one-time-fetch" scenarios the other day. Using contexts makes sense but I still feel like it's an overkill if the data is not consumed by multiple components. The other workaround I could think of is making use of localStorage to:

  • persist data for the full lifetime of the app(even if it's closed, but we clan clean up the storage upon closing the app)
  • access the data from the component where it's needed(note it's a single component)
  • avoid re-fetching the data if it's already presented in the storage

It comes with the cost of a somewhat more complex logic(e.g. each time a certain component mounts we should check for the data's existence) but it's worth the "effort" imo.

Could you perceive other advantages/disadvantages?

@thisismydesign
Copy link
Member

Why are contexts an overkill though?

@gomorizsolt
Copy link
Collaborator Author

Forget the proposal above, it seems it's indeed one of the best-practices of using contexts.

Use React's Context API for things that are truly necessary deep in the react tree. They don't have to be things you need everywhere in the application (you can render a provider anywhere in the app). This can really help avoid some issues with prop drilling. It's been noted that context is kinda taking us back to the days of global variables. The difference is that because of the way the API was designed, you can still statically find the source of the context as well as any consumers with relative ease.

Source: https://kentcdodds.com/blog/prop-drilling

@gomorizsolt gomorizsolt marked this pull request as ready for review December 30, 2019 08:48
@thisismydesign
Copy link
Member

Closing in favor of ca945f6 and c-hive/basics#8

Thanks for gathering use cases in this PR!

@thisismydesign thisismydesign deleted the react-best-practices#contexts branch October 1, 2020 11:20
@thisismydesign thisismydesign restored the react-best-practices#contexts branch October 1, 2020 11:20
@thisismydesign thisismydesign deleted the react-best-practices#contexts branch October 1, 2020 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants