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

feat(useState): allow passing new state directly #34

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

Conversation

devhammed
Copy link

Currently, you need to write a function to setState but this PR allows passing the state value directly just like in React:

/** @jsx Didact.createElement */
function Counter() {
  const [state, setState] = Didact.useState(1)

  return (
    <div>
      <p>Count: {state}</p>
      <button onClick={() => setState(c => c - 1)}>Decrement</button>
      <button onClick={() => setState(0)}>Reset</button>
      <button onClick={() => setState(c => c + 1)}>Increment</button>
    </div>
  )
}
const element = <Counter />
const container = document.getElementById("root")
Didact.render(element, container)

Currently, you need to write a function to setState but this PR allows passing the state value directly just like in React:

```jsx
/** @jsx Didact.createElement */
function Counter() {
  const [state, setState] = Didact.useState(1)

  return (
    <div>
      <p>Count: {state}</p>
      <button onClick={() => setState(c => c - 1)}>Decrement</button>
      <button onClick={() => setState(0)}>Reset</button>
      <button onClick={() => setState(c => c + 1)}>Increment</button>
    </div>
  )
}
const element = <Counter />
const container = document.getElementById("root")
Didact.render(element, container)
```
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.

None yet

1 participant