When using useState in Preact (or React), the setState function doesn't accept a callback as a second parameter, unlike in some other libraries. Instead, the proper way to execute code after a state update is to use the useEffect hook, which reacts to changes in state.
Source code viewer
import { h } from 'preact'; import { useState, useEffect, useLayoutEffect } from 'preact/hooks'; const MyComponent = () => { const [count, setCount] = useState(0); // useEffect will run after the state change has been committed to the DOM useEffect(() => { console.log(`useEffect: Count has been updated to: ${count}`); }, [count]); // useLayoutEffect will run synchronously after the DOM has been updated but before the browser has painted useLayoutEffect(() => { console.log(`useLayoutEffect: Count has been updated to: ${count}`); }, [count]); return ( <div> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); }; export default MyComponent;Programming Language: ECMAScript