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

Fix useState to support primitive values in setState #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aym-sekiguchi
Copy link

Description

Currently, useState throws an error when a primitive value (e.g., a number or string) is passed to setState. This happens because setState expects a function but directly assigns the value, causing an issue when calling action(hook.state).

This fix ensures that primitive values are correctly handled by converting them into a function before storing them in the queue. This makes useState behave more like React's implementation.

Changes

  • Updated setState to check if action is a function.
  • If action is a primitive value, it is wrapped in a function: (prev) => action.
  • This prevents errors and aligns Didact with React's behavior.

Before Fix (Throws an Error)

const [count, setCount] = Didact.useState(0);
setCount(1); // ❌ Error: action is not a function

After Fix (Works Correctly)

const [count, setCount] = Didact.useState(0);
setCount(1); // ✅ No error
setCount((prev) => prev + 1); // ✅ Works as expected

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