-
-
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]
useStore()
and other improvements (#5205)
* Add changesets, export createStoreConfig, useStore * Fix typo * Remove NoInfer * Update jsdoc
- Loading branch information
1 parent
08ade1a
commit 65784ae
Showing
9 changed files
with
370 additions
and
24 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,29 @@ | ||
--- | ||
'@xstate/store': minor | ||
--- | ||
|
||
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. | ||
|
||
```tsx | ||
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); | ||
|
||
// ... | ||
} | ||
``` |
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,37 @@ | ||
--- | ||
'@xstate/store': minor | ||
--- | ||
|
||
There is now a `useStore()` hook that allows you to create a local component store from a config object. | ||
|
||
```tsx | ||
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> | ||
); | ||
} | ||
``` |
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,5 @@ | ||
--- | ||
'@xstate/store': patch | ||
--- | ||
|
||
The `createStoreWithProducer(config)` function now accepts an `emits` object. |
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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
export { shallowEqual } from './shallowEqual'; | ||
export { fromStore } from './fromStore'; | ||
export { createStore, createStoreWithProducer } from './store'; | ||
export { | ||
createStore, | ||
createStoreWithProducer, | ||
createStoreConfig | ||
} from './store'; | ||
export * from './types'; |
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
Oops, something went wrong.