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

[@xstate/store] useStore() and other improvements #5205

Merged
merged 4 commits into from
Feb 21, 2025

Conversation

davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Feb 17, 2025

There is now a useStore() hook that allows you to create a local component store from a config object.

import { useStore, useSelector } from '@xstate/store/react';

function Counter() {
  const store = useStore({
    context: {
      name: 'David',
      count: 0
    },
    on: {
      inc: (ctx, { by }: { by: number }) => ({
        ...ctx,
        count: ctx.count + by
      })
    }
  });
  const count = useSelector(store, (state) => state.count);

  return (
    <div>
      <div>Count: {count}</div>
      <button onClick={() => store.trigger.inc({ by: 1 })}>
        Increment by 1
      </button>
      <button onClick={() => store.trigger.inc({ by: 5 })}>
        Increment by 5
      </button>
    </div>
  );
}

Added createStoreConfig to create a store config from an object. This is an identity function that returns the config unchanged, but is useful for type inference.

const storeConfig = createStoreConfig({
  context: { count: 0 },
  on: { inc: (ctx) => ({ ...ctx, count: ctx.count + 1 }) }
});

// Reusable store config:

const store = createStore(storeConfig);

// ...
function Comp1() {
  const store = useStore(storeConfig);

  // ...
}

function Comp2() {
  const store = useStore(storeConfig);

  // ...
}

The createStoreWithProducer(config) function now accepts an emits object (fix).

Copy link

changeset-bot bot commented Feb 17, 2025

🦋 Changeset detected

Latest commit: 9d0c0de

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

This PR includes changesets to release 1 package
Name Type
@xstate/store 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

Comment on lines +13 to +28
// Reusable store config:

const store = createStore(storeConfig);

// ...
function Comp1() {
const store = useStore(storeConfig);

// ...
}

function Comp2() {
const store = useStore(storeConfig);

// ...
}
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't one just use a reusable hook in this case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I probably need to make this a better example

@davidkpiano davidkpiano merged commit 65784ae into main Feb 21, 2025
1 check passed
@davidkpiano davidkpiano deleted the davidkpiano/usestore-1 branch February 21, 2025 16:58
@github-actions github-actions bot mentioned this pull request Feb 21, 2025
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