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
Update how to reset state doc #1495
Conversation
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 4816067:
|
@dai-shi @KevinMusgrave here's the PR :) |
@chrisk-7777 would you mind reviewing this PR? |
Let's have @devanshj eyes on this, in case there are better solutions. |
Should the curry workaround have a mention? It allows multiple middleware. // Use a trailing comma after `T` in tsx files.
const create = <T,>() => <
Mos extends [StoreMutatorIdentifier, unknown][] = []
>(
initializer: StateCreator<T, [], Mos>
) => {
const slice = _create(initializer);
const initialSlice = slice.getState();
resetters.push(() => {
slice.setState(initialSlice, true);
});
return slice;
}; const useSalmon = create<StateSalmon & ActionsSalmon>()(
immer(
devtools((set, get) => ({
// ...
}))
)
); |
Do you know if there's a way to avoid that formatting tools, like prettier, remove that trailing comma? |
@dbritto-dev here's a couple ideas: Option 1.Use - ```tsx
+ ```ts Option 2.Tell prettier to skip formatting + /* prettier-ignore */
const create = <T,>() => < Option 3.Type - const create = <T,>() => <
+ const create = <T extends unknown> => < Option 4.Tell eslint we know + /* eslint-disable @typescript-eslint/no-unused-vars */
- const create = <T,>() => <
+ const create = <T,_>() => < Option 5.Use a function expression (maybe not so consistent with other doc pages). This would mimic the declaration file for - const create = <T,>() => <
- Mos extends [StoreMutatorIdentifier, unknown][] = []
- >(
+ const create = function <T>() {
+ return <Mos extends [StoreMutatorIdentifier, unknown][] = []>(
initializer: StateCreator<T, [], Mos>
) => {
const slice = _create(initializer);
const initialSlice = slice.getState();
resetters.push(() => {
slice.setState(initialSlice, true);
});
return slice;
+ };
}; Option 6.Pull up the - const create = <T,>() => <
- Mos extends [StoreMutatorIdentifier, unknown][] = []
+ const create = <T, _Mos extends [StoreMutatorIdentifier, unknown][] = []>() => <
+ Mos extends [StoreMutatorIdentifier, unknown][] = _Mos Imo we shouldn't try to replicate the default export TLDR: use option 6, don't overload. |
To make prettier happy without changing configs, I prefer option 3. It means |
Yeah, sorry about the typo. Can you double check? There was also typo where I said option "D"... and all the options are numbered. I also added option 6, but the type of |
I mean this. - const create = <T,>() => <
+ const create = <T extends unknown> => < |
Does changing "ts" to "tsx" after three backquotes help? |
Yes, but anyone who implements it in a tsx file will have the problem if they're also using prettier. The codesandboxes linked in #1494 autoformat on save, breaking the example. |
Changing the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type of create
here has the same type as the original create
, so there's no need of retyping it (the retyping is also incorrect as it misses the Mis
type parameter), so one should just use typeof originalCreate
. And it should also implement the curried usage.
So the final code would look something like this... (feel free to change formating, variable names, etc)
import _create, { StateCreator } from 'zustand'
const resetters: (() => void)[] =
[]
export const create = (<T extends unknown>(f: StateCreator<T> | undefined) => {
if (f === undefined) return create
const store = _create(f)
const initialState = store.getState()
resetters.push(() => {
store.setState(initialState, true)
})
return store
}) as typeof _create
export const resetAllSlices = () => {
for (const resetter of resetters) {
resetter()
}
}
Also the code snippet above this one should use the curried version ie create<State & Actions>()(...)
instead of create<State & Actions>(...)
. The latter is not anywhere in the TypeScript docs (except for "create without curried workaround" section) and is not recommended.
Thanks I'll apply the changes |
@devanshj the changes are done :D |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left an addtional suggestion as a comment.
And let's also change create<State & Actions>(...
into create<State & Action>()(...
in the first snippet of the document as I suggested earlier (can't comment on that line as it's not part of the change).
Other than these changes it looks good.
@devanshj the changes are done |
This one is still missing:
|
yeah, I updated the first codesandbox demo as well. Now we are good. |
No I'm talking about line 30 in how-to-reset-state.md, that still needs a change, let me know if it's still not clear. |
I see. I thought you were talking about the codesandbox demos haha, my bad |
@devanshj all yours |
@dai-shi all yours :D |
Related Issues
Fixes #1494
Summary
Check List
yarn run prettier
for formatting code and docs