-
-
Notifications
You must be signed in to change notification settings - Fork 51
Updated setState to support updateFn to work with both updater function and state #204
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
base: main
Are you sure you want to change the base?
Conversation
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.
I just wanted to share a quick thought based on our Discord conversation.
I'm not a maintainer of this project. Just another community member sharing some technical observations.
Thanks for taking the time to consider my perspective in detail!
} else { | ||
this.state = updater as TState | ||
} | ||
this.state = this.options?.updateFn ? this.options.updateFn(this.prevState)(() => updater) : updater |
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.
When updater is a function, updater: (prev: TState) => TState
this.options.updateFn(this.prevState)(() => updater)
The type of () => updater is () => (prev: TState) => TState, but updateFn expects a parameter of type (prev: TState) => TState.
When updater is a value, updater: TState
this.options.updateFn(this.prevState)(() => updater)
The type of () => updater is () => TState, but updateFn expects a parameter of type (prev: TState) => TState.
It seems like this might cause a type error. Am I missing something?
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.
setState(updater: TState | ((prevState: TState) => TState)): void {
this.prevState = this.state
if (this.options?.updateFn) {
// When updateFn is defined: always convert the updater to a function.
const functionUpdater = isUpdaterFunction(updater)
? updater
: (prev: TState) => updater
this.state = this.options.updateFn(this.prevState)(functionUpdater)
} else {
// When updateFn is not defined: handle the updater directly.
this.state = isUpdaterFunction(updater)
? updater(this.prevState)
: updater
}
}
If I were to make changes, I think this could be a possible improvement. What do you think?
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.
So, we need to check both whether updater
is a function or not and updateFn
is defined or not. Either way is fine: we can check updater
’s function type first and then updateFn
, or vice versa.
If you have the time, I’d really appreciate hearing your thoughts on this, @tannerlinsley. |
Currently
updateFn
is throwing an error when trying to setState directly without using function. e.g.Updated setState to support that. Also we should not allow user to update Updater type so, remove
TUpdater
.