Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mainendra
Copy link

@mainendra mainendra commented Jun 2, 2025

Currently updateFn is throwing an error when trying to setState directly without using function. e.g.

const countStore = new Store(1, {
    updateFn: prevState => updater => {
        return updater(prevState) + prevState;
    }
});
countStore.setState(1) // error
countStore.setState(() => 1) // success

Updated setState to support that. Also we should not allow user to update Updater type so, remove TUpdater.

@mainendra mainendra marked this pull request as draft June 2, 2025 15:43
@mainendra mainendra changed the title Draft: Updated setState to support both updater function and state Draft: Updated setState to support updateFn to work with both updater function and state Jun 4, 2025
@mainendra mainendra closed this Jun 4, 2025
@mainendra mainendra reopened this Jun 4, 2025
@mainendra mainendra marked this pull request as ready for review June 4, 2025 01:45
@mainendra mainendra changed the title Draft: Updated setState to support updateFn to work with both updater function and state Updated setState to support updateFn to work with both updater function and state Jun 4, 2025
Copy link
Contributor

@jiji-hoon96 jiji-hoon96 left a 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
Copy link
Contributor

@jiji-hoon96 jiji-hoon96 Jun 4, 2025

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?

Copy link
Contributor

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?

Copy link
Author

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.

@jiji-hoon96
Copy link
Contributor

If you have the time, I’d really appreciate hearing your thoughts on this, @tannerlinsley.
I value your perspective as the maintainer of the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants