-
-
Notifications
You must be signed in to change notification settings - Fork 378
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
Try state in context with useHook #796
Conversation
Deploy preview for reakit ready! Built with commit 8dfd5e4 |
Size Change: +1.15 kB (0%) Total Size: 258 kB
ℹ️ View Unchanged
|
@diegohaz This is an alternative implementation that is not hacking into the And it is using |
Deploy preview for reakit ready! Built with commit 22fe86c |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 3937cf9:
|
Deploy preview for reakit ready! Built with commit 3937cf9 |
3f64099
to
50eaa29
Compare
Codecov Report
@@ Coverage Diff @@
## master #796 +/- ##
==========================================
- Coverage 95.12% 94.80% -0.33%
==========================================
Files 231 237 +6
Lines 3510 3581 +71
Branches 955 967 +12
==========================================
+ Hits 3339 3395 +56
- Misses 170 184 +14
- Partials 1 2 +1
Continue to review full report at Codecov.
|
@diegohaz @tom-sherman Just finished this up 😄 Tests are passing. I'll experiment with other components as well. Would like to hear your thoughts about this implementation. |
Amazing work @david-szabo97 🚀 I currently don't have time to review Reakit PRs (I work on react native right now and am currently trying to fight off burnout in these trying times, so avoiding out of hours projects) - I've removed myself from the review request - hope you don't mind! |
This looks pretty slick, and definitely way better for userland in general. I will give the tires a kick on this on our functional design system sometime next week to see how it feels. |
@brianespinosa Thanks! That will help a lot. I'm more focused on v2 right now, but it would be awesome to have this feature on v1 as well. David has done an excellent job. |
@diegohaz do we have an alpha or nightly build version for v2 that we can take a look at yet? I did not realize that was coming. |
Not yet! I'm still experimenting with some different implementations to see if/how they work in real projects, especially considering the new concurrent rendering features on React. Once I have something functional I'll create a branch here and propose the changes. I expect this will take a few months, but I hope to have an alpha version or something by the end of the year. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Resolves: #745
Alternative implementation to #792, I felt like #792 more of a hack rather than a solution. That's why I moved on and tried to come up with a solution that doesn't change the core and more reusable.
Benefits
Goals
Is there anything we need to do to enable this feature for components?
Yes
The PR introduces two new hooks:
useStateContextConsumer
anduseStateContextProvider
. To create a context you can use the helper functioncreateStateContext
.useStateContextProvider
This hook should be used by the parent component. For example
ComboboxPopover
is a component that containsComboboxOption
. In this case,ComboboxPopover
is the parent component and it should provide the context.First, you will need a context:
Then you need to add the hook to the
compose
array as the very first value:If your component is using
useComposeProps
too, then you need to add the hook there as well:useStateContextConsumer
This hook should be used by the child component. For example
ComboboxPopover
is a component that containsComboboxOption
. In this case,ComboboxOption
is the child component and it should consume the context.You should create a context in your parent component as mentioned above. Then you need to add the hook to the
compose
array as the very first value:useStateContextConsumer
takes an object with 3 properties:context
the context that you would like to consume, this should be the context that your parent component providesshouldUpdate
a function that returns whether the component should update its state or not. This function gives you 3 parameters:id
of the component,state
current state of the component andnextState
which is the state that we just received from the context. If we returntrue
, then thenextState
will be applied to the component.updateDependencies
under the hood, the pubsub system takes some dependencies. According to these dependencies, your component resubscribes to the pubsub system. This makes sure you receive correct values inshouldUpdate
.How to test?
npm run storybook
Combobox -> Combobox Non Context Vs Context
Does this PR introduce breaking changes?
Hopefully not!