React useState Hook vs. Solid createSignals Hook

Emir AKAR
ÇSTech
Published in
5 min readJul 4, 2023

In the world of modern web development, building interactive user interfaces is a core aspect. JavaScript frameworks and libraries offer various tools to simplify the process, and React has been a popular choice due to its component-based architecture. React provides hooks, which are functions that allow developers to add stateful logic to functional components. One of the most widely used hooks is useState. However, a relatively new framework called Solid introduces a different approach with its createSignals hook. In this article, we will compare React’s useState hook with Solid’s createSignals hook, exploring their similarities, differences, and use cases.

React’s useState Hook:

React’s useState hook is a fundamental tool for enabling state management within functional components, offering a declarative approach that facilitates tracking and handling of component state changes. By utilizing useState, developers can declare and update state variables directly within their components, with the function returning a tuple comprising the current state value and an update function. Consequently, React automatically re-renders the component whenever the state changes, ensuring a reactive user interface. This aligns with React’s adoption of unidirectional data flow, wherein data flows from parent components to child components. Through this paradigm, the parent component takes responsibility for managing the state and transmits it to child components as props, enabling the child components to update the state by invoking the provided update function.

Let’s understand how react useState work with an example;

Unidirectional Data Flow With React UseStates
Unidirectional Data Flow With React UseStates
import { FC, useState } from "react";
import { ChildOne } from "./components/child-one";
import { ChildTwo } from "./components/child-two";
import { ChildThree } from "./components/child-three";

import "./App.css";

const App: FC = () => {
const [counter, setCounter] = useState(0);

const handleCounter = () => {
setCounter(counter + 1);
console.log("Clicked! (React)", counter + 1);
};

return (
<div className="App">
<button onClick={handleCounter}>Parent</button>
<div>
<ChildOne />
<ChildTwo />
<ChildThree counter={counter} />
</div>
</div>
);
};

export default App;
Console Logs of React UseStates

Solid’s createSignals Hook:

Solid, a declarative and reactive JavaScript library, introduces the createSignals hook as an alternative to React’s useState. This hook is built on the concept of signals, which are observable values that can hold both synchronous and asynchronous data. By subscribing to signals, components in Solid automatically re-render whenever the subscribed signals change. This reactive data flow model enables fine-grained reactivity without the need for explicit instructions on how or when to update, reducing the explicit data passing between components. Solid’s createSignals hook and its reactive data flow paradigm provide developers with greater flexibility and efficiency in state management.

Let’s understand how solid createSignals work with an example;

Reactive Data Flow With Solid CreateSignals
import { createSignal, type Component } from "solid-js";
import { ChildOne } from "./components/child-one";
import { ChildTwo } from "./components/child-two";
import { ChildThree } from "./components/child-three";

import "./App.css";

const App: Component = () => {
const [counter, setCounter] = createSignal(0);

const handleCounter = () => {
setCounter(counter() + 1);
console.log("Clicked! (Solid)", counter());
};

return (
<div class="App">
<button onClick={handleCounter}>Parent</button>
<div>
<ChildOne />
<ChildTwo />
<ChildThree counter={counter()} />
</div>
</div>
);
};

export default App;
Console Logs of Solid CreateSignals

Similarities:

Both React’s useState and Solid’s createSignals hooks share the goal of managing state within functional components. They provide a mechanism for storing and updating values that trigger re-renders. This similarity enables developers to build interactive and responsive user interfaces in both frameworks. Both hooks also follow a declarative approach, emphasizing the separation of concerns and making the code easier to reason about.

Differences:

  1. React’s useState hook is based on direct variable assignment, while Solid’s useSignals hook operates with signals that hold values. React’s approach may be more familiar to developers already experienced with JavaScript, while Solid’s signals introduce a new paradigm.
  2. Solid’s createSignals hook offers more granular reactivity compared to React’s useState. With signals, developers can create fine-grained dependencies, ensuring that components only update when relevant signals change. This level of reactivity can improve performance and prevent unnecessary re-renders.

Use Cases:

React’s useState hook is well-suited for most use cases, especially when building small to medium-sized applications with straightforward state management requirements. It's simplicity and wide adoption make it an excellent choice for many developers.

Solid’s createSignals hook shines in scenarios where fine-grained reactivity and more complex state management are essential. Applications with extensive data flows, asynchronous operations, or a need for high performance can benefit from Solid’s signals-based approach. It offers a reactive programming model that can handle intricate data dependencies efficiently.

Conclusion:

Both React’s useState hook and Solid’s useSignals hook serve as powerful state management tools for functional components. React’s useState provides a straightforward and widely adopted approach, while Solid’s createSignals introduces a more granular and reactive paradigm. The choice between the two ultimately depends on the specific requirements of your project. React remains a popular choice for its simplicity and extensive ecosystem, while Solid offers advanced reactivity and flexibility for complex applications. Exploring and understanding the strengths of each hook will help developers make informed decisions when building interactive user interfaces.

References & Resources:

--

--

Emir AKAR
ÇSTech
Writer for

Hi there! I am Emir. I am a curious Frontend Developer who likes to improve himself in different fields. I like using JavaScript, HTML, CSS, and React JS.