Skip to content
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

Added createFacetContext #86

Merged
merged 2 commits into from
Aug 23, 2022
Merged

Added createFacetContext #86

merged 2 commits into from
Aug 23, 2022

Conversation

Shenato
Copy link
Contributor

@Shenato Shenato commented Aug 22, 2022

This PR adds a new facet function createFacetContext

Which can be used to create a context that holds a facet with the type Facet instead of WritableFacet.

NOTE: Writable facets you provide into the context provider are still technically writable as you can call them with .set() however the function will change the type to show that the facet doesn't have a setter internally (in reality it does).

This hopefully will help steer consumers away from setting facets arbitrarily using createFacet and facet.set() instead of sticking to more conventional react methods and or useFacetX hooks

this turns code like this

const myFancyContext = createContext<Facet<number | undefined>>(createFacet({ initialValue: undefined }))
export const useMyFancyContext = () => useContext(myFancyContext)

into

const myFancyContext = createFacetContext<number | undefined>(undefined)
export const useMyFancyContext = () => useContext(myFancyContext)

In the first instance the Facet was a WritableFacet and was gonna stay that way no matter what, in the 2nd instance it is no longer a WritableFacet, which hopefully should offer a cleaner method of writing a context holding a facet for it's property of not triggering reconciliation and not for it's property of being a writable store of value.

@Shenato Shenato requested a review from pirelenito August 22, 2022 15:09
Copy link
Member

@pirelenito pirelenito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff! 👏

@Shenato Shenato merged commit 8d63dbd into main Aug 23, 2022
@Shenato Shenato deleted the create-facet-context branch August 23, 2022 11:59
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