-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@xstate/store] Compatibility with
@xstate/react
's useSelector(…)
…
… hook (#4844) * Make `@xstate/store` compatible with `@xstate/react`'s `useSelector(…)` hook * Add changeset * Add additional tests * Add Vue compatibility * Svelte * Update .changeset/tall-bobcats-trade.md Co-authored-by: Mateusz Burzyński <[email protected]> --------- Co-authored-by: Mateusz Burzyński <[email protected]>
- Loading branch information
1 parent
9118720
commit 5aa6eb0
Showing
10 changed files
with
306 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
'@xstate/react': patch | ||
'@xstate/svelte': patch | ||
'@xstate/vue': patch | ||
--- | ||
|
||
The `useSelector(…)` hook from `@xstate/react` is now compatible with stores from `@xstate/store`. | ||
|
||
```tsx | ||
import { createStore } from '@xstate/store'; | ||
import { useSelector } from '@xstate/react'; | ||
|
||
const store = createStore( | ||
{ | ||
count: 0 | ||
}, | ||
{ | ||
inc: { | ||
count: (context) => context.count + 1 | ||
} | ||
} | ||
); | ||
|
||
function Counter() { | ||
// Note that this `useSelector` is from `@xstate/react`, | ||
// not `@xstate/store/react` | ||
const count = useSelector(store, (state) => state.context.count); | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => store.send({ type: 'inc' })}>{count}</button> | ||
</div> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<template> | ||
<div data-testid="count">{{ snapshot.context.count }}</div> | ||
<button data-testid="increment" @click="send({ type: 'inc' })"> | ||
Increment | ||
</button> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent } from 'vue'; | ||
import { useActor } from '@xstate/vue'; | ||
import { fromStore } from '../src/index.ts'; | ||
export default defineComponent({ | ||
emits: ['rerender'], | ||
setup() { | ||
const { snapshot, send } = useActor(fromStore({ | ||
count: 0 | ||
}, { | ||
inc: (ctx) => ({ count: ctx.count + 1 }) | ||
})) | ||
snapshot.value.context.count satisfies number; | ||
return { snapshot, send }; | ||
} | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<template> | ||
<div data-testid="count">{{ count }}</div> | ||
<button data-testid="increment" @click="actorRef.send({ type: 'inc' })"> | ||
Increment | ||
</button> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { Ref, defineComponent } from 'vue'; | ||
import { useActorRef, useSelector } from '@xstate/vue'; | ||
import { fromStore } from '../src/index.ts'; | ||
export default defineComponent({ | ||
emits: ['rerender'], | ||
setup() { | ||
const actorRef = useActorRef(fromStore({ | ||
count: 0 | ||
}, { | ||
inc: (ctx) => ({ count: ctx.count + 1 }) | ||
})) | ||
const count = useSelector(actorRef, s => s.context.count) | ||
count satisfies Ref<number>; | ||
return { count, actorRef }; | ||
} | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<template> | ||
<div data-testid="count">{{ count }}</div> | ||
<button data-testid="increment" @click="store.send({ type: 'inc' })"> | ||
Increment | ||
</button> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { Ref, defineComponent } from 'vue'; | ||
import { useSelector } from '@xstate/vue'; | ||
import { createStore } from '../src/index.ts'; | ||
export default defineComponent({ | ||
emits: ['rerender'], | ||
setup() { | ||
const store = createStore({ | ||
count: 0 | ||
}, { | ||
inc: (ctx) => ({ count: ctx.count + 1 }) | ||
}) | ||
const count = useSelector(store, (state) => state.context.count); | ||
count satisfies Ref<number>; | ||
return { store, count }; | ||
} | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { fireEvent, render } from '@testing-library/vue'; | ||
import UseSelector from './UseSelector.vue'; | ||
import UseActor from './UseActor.vue'; | ||
import UseActorRef from './UseActorRef.vue'; | ||
|
||
it('works with `useSelector(…)` (@xstate/vue)', async () => { | ||
const { getByTestId } = render(UseSelector); | ||
|
||
const countEl = getByTestId('count'); | ||
const incrementEl = getByTestId('increment'); | ||
|
||
await fireEvent.click(incrementEl); | ||
expect(countEl.textContent).toBe('1'); | ||
}); | ||
|
||
it('works with `useActor(…)` (@xstate/vue)', async () => { | ||
const { getByTestId } = render(UseActor); | ||
|
||
const countEl = getByTestId('count'); | ||
const incrementEl = getByTestId('increment'); | ||
|
||
await fireEvent.click(incrementEl); | ||
expect(countEl.textContent).toBe('1'); | ||
}); | ||
|
||
it('works with `useActorRef(…)` (@xstate/vue)', async () => { | ||
const { getByTestId } = render(UseActorRef); | ||
|
||
const countEl = getByTestId('count'); | ||
const incrementEl = getByTestId('increment'); | ||
|
||
await fireEvent.click(incrementEl); | ||
expect(countEl.textContent).toBe('1'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters