Your code works but the problem is after initial toggle, condition get stuck in true
hence calling only trueFunc
when the button gets clicked.
Here is how you can do it in a clear manner:
import { render } from "solid-js/web";
import { createSignal, createEffect } from "solid-js";
function Counter() {
const [flag, setFlag] = createSignal(false);
const handleClick = () => setFlag(!flag());
createEffect(() => {
console.log(flag());
});
return (
<>
<button onClick={handleClick}>Flag: {String(flag())}</button>
</>
);
}
render(() => <Counter />, document.getElementById("app")!);
We toggle the flag directly inside the click handler:
const handleClick = () => setFlag(!flag());
Another way to do it would be using a callback inside the setter function:
const handleClick = () => setFlag(v => !v);
We used an effect to observe and print the flags value:
createEffect(() => {
console.log(flag());
});