-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat(component-store): add patchState method #2788
Conversation
Preview docs changes for 2c2fc55 at https://previews.ngrx.io/pr2788-2c2fc556/ |
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.
It keeps surprising (in a good way) me that this code "is so simple" 👍
it( | ||
'with a value based on the previous state', | ||
marbles((m) => { | ||
componentStore.patchState(() => ({ value2: { foo: 'fooBar' } })); |
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.
Should this test use the current state to build a new state?
e.g.
componentStore.patchState(() => ({ value2: { foo: 'fooBar' } })); | |
componentStore.patchState((state) => ({ value2: { foo: state.value2.foo + "2" } })); |
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.
sure 🙂
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.
Thanks for working on this 👍
patchState( | ||
partialStateOrUpdaterFn: Partial<T> | ((state: T) => Partial<T>) | ||
): void { | ||
if (!this.isInitialized) { |
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.
updater (and setState that calls the updater) does this check, so it's not needed here.
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.
done
throw new Error(this.notInitializedErrorMessage); | ||
} | ||
|
||
const updaterFn = (state: T) => ({ |
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.
a few suggestions:
- We can use
this.setState
here - Let's extract and evaluate
patchedState
separately for readability purposes.
const updaterFn = (state: T) => ({ | |
this.setState((state) => { | |
const patchedState = (typeof partialStateOrUpdaterFn === 'function') | |
? partialStateOrUpdaterFn(state) | |
: partialStateOrUpdaterFn; | |
return { | |
...state, | |
...patchedState, // 👈 comes after `state` to override the properties | |
}; | |
} |
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.
done
667c291
to
2c2fc55
Compare
* feat(component-store): add patchState method * use setState in patchState; remove isInitialized check; improve test case with callback
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Closes #2786
What is the new behavior?
Does this PR introduce a breaking change?