Skip to content

๐Ÿ”„ What is setEffect? โ€‹

setEffect is a reactive side-effect hook in SeraJS.

It automatically runs the provided function whenever any signal used inside it changes โ€” no need to specify dependencies manually!

js
setEffect(() => {
  // reactive logic
});

๐Ÿš€ Example App โ€‹

Here's a practical counter example that logs the value of count every time it changes:

js
import { h, setSignal, Fragment, setEffect } from "serajs";

export default function App() {
  const [count, setCount] = setSignal(0);

  setEffect(() => {
    console.log(count()); // Automatically runs when count changes
  });

  return (
    <>
      <h1>{() => count()}</h1>
      <button onClick={() => setCount(count() + 1)}>Increment Count</button>
      <button onClick={() => setCount(count() - 1)}>Decrement Count</button>
      <button onClick={() => setCount(0)}>Reset Count</button>
    </>
  );
}

๐Ÿง  How it Works โ€‹

  • setEffect tracks any signal (count() in this case) accessed inside its function.
  • When setCount() updates the signal, the effect runs again automatically.
  • โœ… No need to manage dependency arrays like in React.

๐Ÿ›  Best Practices โ€‹

  • Use setEffect for:

    • Logging
    • Fetching data
    • Syncing localStorage
    • Any other side-effect logic
  • Avoid placing setEffect inside JSX. Keep it at the top level of your component.


๐Ÿงช Output Example โ€‹

Every time you click a button:

0
1
2
1
0
1
2
3
4
5

โœ… Summary โ€‹

FeatureBehavior
Auto-runsWhen used signals change
No depsNo need for a dependency array
SimplicityClean, readable, and reactive