c-hive / guides

Guides for code style, way of working and other development concerns
MIT License
26 stars 5 forks source link

Context best-practices #34

Closed gomorizsolt closed 4 years ago

gomorizsolt commented 4 years ago

Looking for ideas..

thisismydesign commented 4 years ago

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

gomorizsolt commented 4 years ago

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:

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 commented 4 years ago

Why are contexts an overkill though?

gomorizsolt commented 4 years ago

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

thisismydesign commented 4 years ago

Closing in favor of https://github.com/c-hive/guides/commit/ca945f67b544921e5b1573d06ada261f8c90ecf4 and https://github.com/c-hive/basics/issues/8

Thanks for gathering use cases in this PR!