storefront-api-examples
storefront-api-examples copied to clipboard
React apollo with context provider
DISCLAIMER: I am not sure if this would qualify as a separate branch, but I would love to discuss if that is something we want to pursue.
Overview
- (Another) Disclaimer: this does add
eslint.rc
and.editorconfig
-- but can be removed. - Creates a
StoreProvider
, where all functions related to the store and apollo mutations will occur. - Creates a
withStoreContext
HOC allowing any component (under a StoreContext Provider) to access pieces of the store, all of the store and to interact with this examples functions exposed. This piece of it could definitely be expanded on if that is of interest (areas like updating customer information). - Refactors some of the code due to this new approach. A few components no longer needed to be classes so they were refactored to functional components. I did NOT convert anything to a Hook, I read through other open issues/PR's and it looked like Hooks might be their own branch. I could create another branch for hooks specifically if that is desired as a future addition, I know, hooks are
hip
.
Reason for PR
I was recently working on a client app, and the cart needed to be interacted with from many different parts of the app. It was so much easier to abstract all of the logic into a provider and access it via the HOC. The current example is perfect for a simple dedicated store, but makes it a little difficult to migrate into an existing project without some major refactoring, especially given my teams linter and the current state of react in 2020.
Real Goal
I would like to help make everyone's dev life easier.
@DrTeePot Alright, should be much easier to review now... sorry about all that.