Skip to content

๐Ÿง  setSignal in SeraJS โ€‹

This guide walks you through how to use setSignal in SeraJS to create reactive state in your UI components.


โœจ What is setSignal? โ€‹

setSignal is a function provided by SeraJS that allows you to create reactive state. It returns a getter and a setter โ€” when the value changes, the UI updates automatically.

js
const [value, setValue] = setSignal(initialValue);
  • value() โ€” a function that returns the current value.
  • setValue(newValue) โ€” updates the value and re-renders any part of the UI that depends on it.

๐Ÿš€ Example App โ€‹

Here's a basic counter example using setSignal:

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

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

  return (
    <Fragment>
      <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>
    </Fragment>
  );
}

๐Ÿ” Explanation โ€‹

  • setSignal(0) creates a reactive signal with an initial value of 0.
  • count() returns the current count.
  • setCount(newValue) updates the value.
  • The <h1> element automatically re-renders when count() changes.

๐Ÿงช Why use setSignal? โ€‹

  • โœ… Reactive and declarative
  • โœ… Minimal and fast
  • โœ… Easy to use in JSX

๐Ÿ›  Tips โ€‹

  • Always use () when accessing the signal value: count()
  • You can use signals inside any reactive expressions in your JSX