onChange + onClick event not batched into same render commit
See original GitHub issueReproduction
https://codesandbox.io/s/preact-setstate-overwrite-input-moy82
Steps to reproduce
(using preact/compat
)
function App() {
console.log("rerender");
let [checked, setChecked] = React.useState(false);
let [, setState] = React.useState(false);
return (
<>
<input
type="checkbox"
checked={checked}
onChange={(e) => {
console.log("onChange", e.target.checked);
setChecked(e.target.checked);
}}
onClick={(e) => {
console.log("onClick", e.target.checked);
// ... or uncomment calls to fix
setState(true);
setState(false);
}}
/>
<br />
Checked? {String(checked)}
</>
);
}
- Try to toggle checkbox, doesn’t work because this happens:
onClick true
rerender
onChange false
So the click was overwritten by the setState
call
- With React:
onClick true
onChange true
rerender
- Without the
setState
calls, it obviously works
Expected Behavior
The checkbox should toggle?
Not sure if this is something that can be fixed on the Preact side or if I need to add a workaround.
Actual Behavior
Nothing happens when clicking the checkbox.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
onclick event not triggered when onchange triggered right ...
I typed in a text into the textarea. Right after typing i click on the button to trigger the onclick event on the...
Read more >Fix the slow render before you fix the re-render - Kent C. Dodds
Every time you click on the button, the Foo function is called, but the DOM that it represents is not re-rendered.
Read more >Data Grid - Events - MUI X
You can subscribe to one of the events emitted by providing an event handler to the grid. The handler is a method that...
Read more >Documentation - SolidJS · Reactive Javascript Library
The first execution of the effect function is not immediate; it's scheduled to run after the current rendering phase (e.g., after calling the...
Read more >Working with React Form and Handling Event | Ibaslogic
React is telling us to add an onChange handler to keep track of any changes in the field. Else, it wouldn't know if...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We do batch all state updates which is why the
onClick
handler only causes one re-render. But having a closer look it seems like our window is smaller compared to React. A quick workaround would be to enlarge the time where updates are batched:Thanks, though I’m not comfortable adding this line in a UI component library 😅