SolidJS: How to React on Property Change<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

SolidJS: How to React on Property Change

Posted on: March 7, 2022

In this article, we will show a couple of ways to react to a change of property with SolidJS. There are many types of reactions:

  • We can recompute another variable. For example, a value is built upon a property.
  • We can execute code. For example, clearing a list or a canvas.

Derived Signal

The first case is called a derived signal. It is a well-documented case where you have a computed value that depends on another. However, the documentation is around derived signals and not props. But, the reality is that it behaves the same way hence the same mindset should be observed. By that, the derived value will automatically be recomputed when the property changes.

If we use an example with a single component that passes a single property, we can change the name every second.

1interface HelloWorldProps {
2 name: string;
5const HelloWorld = (props: HelloWorldProps) => {
6 return <h1>Hello, {}!</h1>;
9const names = ["World", "Test", "You"];
10const App: Component = () => {
11 const [index, setIndex] = createSignal(0);
12 onMount(() => {
13 const id = setInterval(() => {
14 setIndex((prev) => {
15 return prev + 1;
16 });
17 }, 1000);
18 onCleanup(() => clearInterval(id));
19 });
20 return <HelloWorld name={names[index() % names.length]} />;
23render(() => <App />, document.getElementById("app"));

A derived property could be added in the HelloWorld component.

1const HelloWorld = (props: HelloWorldProps) => {
2 const derivedName = () => `[${}]`;
3 return <h1>Hello, {derivedName()}!</h1>;

In this example, the component will have the derivedName function called every time the property is changing. However, the HelloWorld itself is rendered only once. For example, writing this variation:

1const HelloWorld = (props: HelloWorldProps) => {
2 console.log("HelloWorld Rendered");
3 const derivedName = () => {
4 console.log("Derived Name Called");
5 return `[${}]`;
6 };
7 return <h1>Hello, {derivedName()}!</h1>;

The console output looks like this:

1HelloWorld Rendered
2Derived Name Called (67 times)

So, the function is not re-created every second; only the function is called because it derives from the changing property.

Executing code

In the previous case, we were computing a value, but what if you need to execute a piece of code if the a property change? Similar to React, it is possible using an effect. With SignalJS, we need to use createEffect. What is excellent and confusing if you are coming from React Hooks, is that you do not need to explicitly specify in the creation of the effect which variable to look at. SolidJS framework handles the reactivity based on what is being used within the effect.

For example:

1createEffect(() => {
2 document.bgColor = document.bgColor === "red" ? "white" : "red";

The background will flip to red since the effect will be executed once when the HelloWorld is rendered (first execution of the component). What if we want to change the color every time the change? For that, we need to use the inside the createEffect. For example, adding a console.log using the property causes the SignalJS framework to know about the property. And, every change of the property must run the effect and hence make the color swap.

1createEffect(() => {
2 console.log(; // Make the createEffect to be called at every change of the property "name"
3 document.bgColor = document.bgColor === "red" ? "white" : "red";

So, there is a caveat here, what if we want to change the background without using console.log? Removing the console.log makes the component renders once, as we saw earlier. So, you can cheat your way to use the variable without assigning.

1createEffect(() => {
2; // Make the effect to depend on the property
3 document.bgColor = document.bgColor === "red" ? "white" : "red";

However, there is an utility function that can be used that make the code cleaner.

2 on(
3 () =>,
4 () => {
5 document.bgColor = document.bgColor === "red" ? "white" : "red";
6 }
7 )
8 );
9 return <h1>Hello, {derivedName()}!</h1>;

The on function takes a function that returns the property you want to watch. In that example, the function () => cause the effect to be evaluated every time the reference of the name is changing. The second function of the on is the code you would put directly in the createEffect. For one rare time, SolidJS is more complex than React, and the documentation about on covers the case of a signal, not props which is confusing.


You can find all the code in SolidJS offers simplicity by removing the burden of using a Linter to avoid stale data in React by analyzing what is used within the body of the effect or by being smart enough to update the derivative function. The simplicity comes with the caveat that you may expect an effect to be called but it won't. For example, if you debug and use property in the console.log, the effect will suddenly behave differently. Or, if you remove the line in the last example that does nothing to the property, you will end up with a stale background color instead of an alternating one.