-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow optional actor in useSelector
#4231
Conversation
🦋 Changeset detectedLatest commit: c4c49e4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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 c4c49e4:
|
What particular use case(s) are you envisioning here? |
const maybeChild = useSelector(parentActor, s => s.children.child);
// const maybeCount = useSelector(maybeChild ?? createEmptyActor(), s => s?.context.count);
const maybeCount = useSelector(maybeChild, s => s?.context.count); |
Is there a similar method for a non-react environment? const $count = actor.watch((state) => state.context.count) something like this, maybe ? cc: @davidkpiano |
I'd like to introduce something like this: // PSEUDOCODE
import { fromSelector } from 'xstate';
const totalActor = fromSelector(someActor, s => {
return s.context.items.reduce((sum, item) => sum + item.price, 0)
});
totalActor.subscribe(s => s.context /* = the total */) |
useSelector
useSelector
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are changing this in @xstate/react
then we should probably do a similar change in @xstate/vue
and @xstate/svelte
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it for @xstate/vue
but I'm not familiar enough with Svelte to get it working there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but I'm not familiar enough with Svelte to get it working there.
Yeah, me neither. I don't think we even support actorRef
changes in its useSelector
. Let's leave it out for now then
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
…/statelyai/xstate into v5/optional-actor-in-useSelector
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
This PR allows an actor to be
undefined
here, with proper typing: