Skip to content
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

Callback Version of setTheme Passes Itself #277

Closed
ryanagillie opened this issue Mar 19, 2024 · 1 comment · Fixed by #286
Closed

Callback Version of setTheme Passes Itself #277

ryanagillie opened this issue Mar 19, 2024 · 1 comment · Fixed by #286
Assignees
Labels
bug Something isn't working

Comments

@ryanagillie
Copy link

ryanagillie commented Mar 19, 2024

Trying to use the callback version of setTheme and noticed that the callback function is being passed to itself

const { setTheme } = useTheme();

const onClick = () => {
  setTheme((pTheme) => {
    console.log(pTheme);
    return pTheme === 'dark' ? 'light' : 'dark';
  })
}

return (
  <button type="button" onClick={onClick}>Click me!</button>
)

image

After digging through the code:
image

this looks to be a simple fix, just need to change 2 things

  1. the useCallback parameter name from theme to something else (value is usually the convention for setState like function), as currently if it's a function it's being called with itself (and not the actual theme, which is defined above)
  2. add theme to callback array (and remove forcedTheme) so it'll always be up to date
@ryanagillie ryanagillie changed the title Callback Version of setTheme Calls Itself Callback Version of setTheme Passes Itself Mar 19, 2024
@trm217 trm217 self-assigned this Apr 23, 2024
@trm217 trm217 added the bug Something isn't working label Apr 23, 2024
@trm217
Copy link
Collaborator

trm217 commented Apr 23, 2024

Thanks for reporting this oversight!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants