Skip to content

Commit 84dba91

Browse files
committedApr 10, 2024·
Update blog
1 parent d39a599 commit 84dba91

File tree

2 files changed

+15
-69
lines changed

2 files changed

+15
-69
lines changed
 

‎.vscode/settings.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"editor.wordWrap": "on"
2+
"editor.wordWrap": "on",
3+
"editor.quickSuggestions": {
4+
"other": "off",
5+
"comments": "off",
6+
"strings": "off"
7+
}
38
}

‎blog/2024-04-10-xstate-store/index.mdx

+9-68
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Introducing XState Store
3-
description: XState Store
3+
description: A simple and small state management library inspired by XState
44
tags:
55
[
66
xstate,
@@ -11,7 +11,6 @@ tags:
1111
authors: [david]
1212
date: 2024-04-10
1313
slug: 2024-04-10-xstate-store
14-
image:
1514
---
1615

1716
Does the world need another state management library? Probably not, but if you've been interested in XState, you're going to want to check this one out.
@@ -63,12 +62,15 @@ store.send({ type: 'inc', by: 2 });
6362
Even in React:
6463

6564
```tsx
65+
// highlight-next-line
6666
import { useSelector } from '@xstate/store/react';
6767
import { store } from './store';
6868

6969
function Counter() {
70+
// highlight-next-line
7071
const count = useSelector(store, (state) => state.context.count);
7172

73+
// highlight-next-line
7274
return <button onClick={() => store.send({ type: 'inc', by: 1 })}>
7375
{count}
7476
</button>;
@@ -240,73 +242,12 @@ const store = createStoreWithProducer(
240242
});
241243
```
242244

243-
## Comparison
244-
245-
XState Store does not claim to be a replacement for any other state management library. The decision for which library to use should be based on your requirements and your team's preferences. Here's a quick comparison of `@xstate/store` with other popular state management libraries, for reference only.
246-
247-
:::info
248-
249-
This is not an exhaustive comparison. It references examples from [the Zustand documentation](https://docs.pmnd.rs/zustand/getting-started/comparison).
250-
251-
:::
252-
253-
### XState Store
254-
255-
```ts
256-
import { createStore } from 'xstate';
257-
258-
const store = createStore({
259-
count: 0
260-
}, {
261-
increment: {
262-
count: (context, event: { qty: number }) => context.count + event.qty
263-
},
264-
decrement: {
265-
count: (context, event: { qty: number }) => context.count - event.qty
266-
}
267-
});
268-
269-
// Use the `useSelector(…)` hook to subscribe to the store in React components
270-
```
271-
272-
### Zustand
273-
274-
```ts
275-
import { create } from 'zustand'
276-
277-
type State = {
278-
count: number
279-
}
280-
281-
type Actions = {
282-
increment: (qty: number) => void
283-
decrement: (qty: number) => void
284-
}
245+
## What's next
285246

286-
const useCountStore = create<State & Actions>((set) => ({
287-
count: 0,
288-
increment: (qty: number) => set((state) => ({ count: state.count + qty })),
289-
decrement: (qty: number) => set((state) => ({ count: state.count - qty })),
290-
}));
291-
```
247+
New features are _not_ planned for `@xstate/store` since it aims to remain small, simple, and focused. However, we would like to add integrations with other frameworks (such as Vue, Angular, Svelte, Solid, etc.) and would greatly appreciate community contributions for those. And we can't forget about examples; keep an eye out for those in the `/examples` directory of the XState repo, such as [this small React counter example](https://github.com/statelyai/xstate/tree/main/examples/store-counter-react).
292248

293-
### Redux Toolkit
249+
Other than that, the next thing for you to do is to try it out! If you've used Zustand, Redux, Pinia, or XState, you'll find `@xstate/store` very familiar. Please keep in mind that you should choose the state management library that best suits your requirements and your team's preferences. However, it is straightforward to migrate to (and from) `@xstate/store` to Redux, Zustand, Pinia, XState, or other state management libraries if needed.
294250

295-
```ts
296-
import { createSlice, configureStore } from '@reduxjs/toolkit'
297-
298-
const countSlice = createSlice({
299-
name: 'count',
300-
initialState: { value: 0 },
301-
reducers: {
302-
incremented: (state, qty: number) => {
303-
state.value += qty
304-
},
305-
decremented: (state, qty: number) => {
306-
state.value -= qty
307-
},
308-
},
309-
})
251+
Our goal with `@xstate/store` is to provide a simple yet powerful _event-based_ state management solution that is type-safe. We believe that indirect (event-based) state management leads to better organization of application logic, especially as it grows in complexity, and `@xstate/store` is a great starting point for that approach.
310252

311-
const countStore = configureStore({ reducer: countSlice.reducer })
312-
```
253+
Give it a try, and feel free to ask any questions in [our Discord](https://discord.gg/xstate) or report bugs in [the XState GitHub repo](https://github.com/statelyai/xstate/issues). We're always looking for feedback on how we can improve the experience!

0 commit comments

Comments
 (0)
Please sign in to comment.