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

Allow optional actor in useSelector #4231

Merged
merged 23 commits into from
Feb 12, 2024
Merged

Conversation

davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Sep 4, 2023

This PR allows an actor to be undefined here, with proper typing:

const someState = useSelector(maybeActor, (snapshot) => ...);

someState; // derived state or undefined

@changeset-bot
Copy link

changeset-bot bot commented Sep 4, 2023

🦋 Changeset detected

Latest commit: c4c49e4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@xstate/react Minor
@xstate/vue Minor

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 4, 2023

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:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

@ghost
Copy link

ghost commented Sep 4, 2023

👇 Click on the image for a new way to code review

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map legend

@with-heart
Copy link
Contributor

What particular use case(s) are you envisioning here?

@davidkpiano
Copy link
Member Author

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);

@Necmttn
Copy link

Necmttn commented Oct 23, 2023

Is there a similar method for a non-react environment?

const $count = actor.watch((state) => state.context.count)

something like this, maybe ?

cc: @davidkpiano

@davidkpiano
Copy link
Member Author

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 */)

@Andarist Andarist changed the base branch from next to main December 1, 2023 11:52
@Andarist Andarist changed the title [v5] Allow optional actor in useSelector Allow optional actor in useSelector Jan 8, 2024
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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

@davidkpiano davidkpiano merged commit c2402e7 into main Feb 12, 2024
2 checks passed
@davidkpiano davidkpiano deleted the v5/optional-actor-in-useSelector branch February 12, 2024 17:46
@github-actions github-actions bot mentioned this pull request Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants