Skip to content

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

@ktm000818
Copy link

"big picture"

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.

3 participants