πŸ’» Programming

Solid.js

Solid

A declarative UI library based on fine-grained reactivity. Provides excellent performance with a React-like syntax through compile-time optimization without a Virtual DOM.

πŸ“– Detailed Explanation

Solid.js is a reactive JavaScript library for building modern declarative UIs. It provides JSX syntax and a component-based architecture similar to React, yet uses a fundamentally different reactivity model. Components run only once, and afterward only the parts that need it are updated precisely by fine-grained reactivity units called Signals. This enables highly efficient UI updates without unnecessary re-rendering.

Solid.js was started by Ryan Carniato in 2018 and officially released in 2021. Carniato is a member of the MarkoJS core team and currently an engineer at Netlify, and has researched reactivity systems for more than 10 years. His philosophy was to "combine React's developer experience with Knockout.js's performance," and Solid.js is the result of achieving that goal. In benchmarks, Solid.js consistently shows performance close to Vanilla JavaScript and outperforms other frameworks by a wide margin.

Fine-grained Reactivity is the core concept of Solid.js. React's Virtual DOM compares the entire component tree when state changes to find the changes, but Solid.js does not need this process. When a Signal changes, only the DOM nodes that subscribe to that Signal are updated directly. This is possible because the dependency graph is analyzed at compile time, so there is almost no runtime overhead. createSignal, createEffect, and createMemo are the basic building blocks of this reactivity system.

In practice, the strengths of Solid.js are performance and bundle size. The bundle size of Solid.js is about 7KB (gzip), much smaller than React (~40KB). It also consistently ranks at the top of the JS Framework Benchmark, delivering outstanding performance especially in large list rendering and frequent updates. Through a meta-framework called SolidStart, it also supports full-stack features such as SSR, routing, and API endpoints. If you are a React developer, the syntax is familiar, so you can gain the performance benefits with a low learning curve.

πŸ’» Code Example

JSX - Signal and Basic Component
import { createSignal } from 'solid-js';

// Signal: Solid.js의 λ°˜μ‘ν˜• μƒνƒœ
// getter ν•¨μˆ˜μ™€ setter ν•¨μˆ˜λ₯Ό λ°˜ν™˜ (React와 닀름!)
function Counter() {
    // countλŠ” ν•¨μˆ˜! count()둜 값을 읽음
    const [count, setCount] = createSignal(0);

    // μ»΄ν¬λ„ŒνŠΈ ν•¨μˆ˜λŠ” ν•œ 번만 싀행됨
    console.log('μ»΄ν¬λ„ŒνŠΈ 생성 - 이 λ‘œκ·ΈλŠ” ν•œ 번만 좜λ ₯!');

    return (
        <div>
            {/* count()λ₯Ό ν˜ΈμΆœν•΄μ•Ό 값을 읽을 수 있음 */}
            <p>카운트: {count()}</p>
            <button onClick={() => setCount(count() + 1)}>
                증가
            </button>
        </div>
    );
}

// Reactμ™€μ˜ 차이점
// React: const [count, setCount] = useState(0); // countλŠ” κ°’
// Solid: const [count, setCount] = createSignal(0); // countλŠ” ν•¨μˆ˜
JSX - Effect and Memo
import { createSignal, createEffect, createMemo } from 'solid-js';

function UserProfile() {
    const [firstName, setFirstName] = createSignal('κΉ€');
    const [lastName, setLastName] = createSignal('철수');

    // createMemo: νŒŒμƒλœ 값을 캐싱 (React의 useMemo와 μœ μ‚¬)
    // firstNameμ΄λ‚˜ lastName이 변경될 λ•Œλ§Œ μž¬κ³„μ‚°
    const fullName = createMemo(() => {
        console.log('fullName 계산 쀑...');
        return `${firstName()} ${lastName()}`;
    });

    // createEffect: λΆ€μˆ˜ 효과 처리 (React의 useEffect와 μœ μ‚¬)
    // μ˜μ‘΄μ„± 배열이 ν•„μš” μ—†μŒ - μžλ™μœΌλ‘œ 좔적!
    createEffect(() => {
        // fullName()을 μ‚¬μš©ν•˜λ©΄ μžλ™μœΌλ‘œ μ˜μ‘΄μ„±μ΄ 좔적됨
        console.log(`이름이 변경됨: ${fullName()}`);
        // 정리(cleanup) ν•¨μˆ˜
        onCleanup(() => console.log('정리 μž‘μ—… μ‹€ν–‰'));
    });

    return (
        <div>
            <input
                value={firstName()}
                onInput={(e) => setFirstName(e.target.value)}
            />
            <input
                value={lastName()}
                onInput={(e) => setLastName(e.target.value)}
            />
            <h2>μ•ˆλ…•ν•˜μ„Έμš”, {fullName()}λ‹˜!</h2>
        </div>
    );
}
JSX - Conditional Rendering and Lists
import { createSignal, Show, For } from 'solid-js';

function TodoList() {
    const [todos, setTodos] = createSignal([
        { id: 1, text: 'Solid.js 배우기', done: false },
        { id: 2, text: 'ν”„λ‘œμ νŠΈμ— μ μš©ν•˜κΈ°', done: false }
    ]);
    const [showCompleted, setShowCompleted] = createSignal(true);

    const toggleTodo = (id) => {
        setTodos(todos().map(todo =>
            todo.id === id ? { ...todo, done: !todo.done } : todo
        ));
    };

    return (
        <div>
            <label>
                <input
                    type="checkbox"
                    checked={showCompleted()}
                    onChange={() => setShowCompleted(!showCompleted())}
                />
                μ™„λ£Œλœ ν•­λͺ© ν‘œμ‹œ
            </label>

            {/* For: 효율적인 리슀트 λ Œλ”λ§ (map λŒ€μ‹  μ‚¬μš©) */}
            <For each={todos()}>
                {(todo) => (
                    {/* Show: 쑰건뢀 λ Œλ”λ§ (μ‚Όν•­μ—°μ‚°μž λŒ€μ‹  ꢌμž₯) */}
                    <Show when={showCompleted() || !todo.done}>
                        <div
                            style={{
                                'text-decoration': todo.done ? 'line-through' : 'none'
                            }}
                            onClick={() => toggleTodo(todo.id)}
                        >
                            {todo.text}
                        </div>
                    </Show>
                )}
            </For>
        </div>
    );
}

πŸ—£ Practical Conversation Example

πŸ‘₯ Tech Selection Meeting: React vs Solid.js
πŸ‘¨β€πŸ’Ό
Tech Lead
We need to decide on the frontend tech stack for the new dashboard project. I've looked into Solid.js. What do you think?
πŸ‘©β€πŸ’»
Senior developer
It's definitely attractive in terms of performance. The dashboard will probably have a lot of real-time charts and data tables, and Solid.js's Fine-grained Reactivity is advantageous for large data updates. In the benchmarks, it's sometimes several times faster than React.
πŸ‘¨β€πŸ’»
Junior Developer
The JSX syntax is similar to React, so I think I could adapt quickly. But I'm worried about the ecosystem... Will there be enough support for chart libraries and UI components?
πŸ‘©β€πŸ’»
Senior developer
Good point. Honestly, it's not as rich as React. But there are component libraries like SolidUI and Hope UI, and for charts you can integrate D3.js directly or use a wrapper like solid-chartjs. The key point is that there may be more parts we have to build ourselves.
πŸ‘¨β€πŸ’Ό
Tech Lead
We also need to think about hiring. There are many React developers, but few with Solid.js experience. Still, the fact that people with React experience can learn it quickly is an advantage. If performance is truly important for the project and the team is open to adopting new technology, I think it's worth trying.

⚑ Performance Optimization Discussion
πŸ‘¨β€πŸ’»
Junior Developer
While migrating from React to Solid.js, I noticed something odd. When I destructure a value from a Signal, reactivity breaks. So I shouldn't do const { name } = user() like this, right?
πŸ‘©β€πŸ’»
Senior developer
That's right, it's an important characteristic of Solid.js. If you extract the value of a Signal in advance, you only get the value at that moment and the reactive connection is broken. You need to access it directly within JSX, like {user().name}. Or you can create a derived Signal with createMemo.
πŸ‘¨β€πŸ’»
Junior Developer
Oh, so that's why the For component receives the item through a callback function? The item itself isn't a Signal, so you have to access it inside the callback to keep reactivity, right?
πŸ‘©β€πŸ’»
Senior developer
Exactly! In Solid.js, reactivity is tracked through function calls. It records which Signals were read at render time, and when those Signals change, only the corresponding parts are updated. That's the secret to efficient updates even without a Virtual DOM.

πŸŽ“ Technical Interview: Framework Comparison
πŸ‘¨β€πŸ’Ό
Interviewer
You said you have experience using Solid.js. What is the biggest difference compared to React?
πŸ‘©β€πŸ’»
Candidate
The biggest difference is the reactivity system. React re-renders the entire component when state changes and compares using the Virtual DOM, whereas Solid.js directly updates only the DOM nodes that use a Signal when it changes. So optimizations like useMemo and useCallback are unnecessary, and performance is good by default.
πŸ‘¨β€πŸ’Ό
Interviewer
Is there anything to watch out for when developing with Solid.js?
πŸ‘©β€πŸ’»
Candidate
Destructuring Signal values breaks reactivity. Out of React habit, you shouldn't do const { name } = user(); you need to access it as user().name inside JSX. Also, the ecosystem is smaller than React's, so there may be parts you have to implement yourself.

πŸ” Code Review: Signal Usage Patterns
πŸ‘¨β€πŸ’Ό
Reviewer
Looking at this code, you're calling createSignal inside the component every time. How are you managing global state?
πŸ‘©β€πŸ’»
Author
Ah, for global state, I define Signals in a separate file and export them. In Solid.js, Signals work outside components too, so you don't need a separate state management library.
πŸ‘¨β€πŸ’Ό
Reviewer
Good. One suggestion: if you wrap these computed values in createMemo, they get cached and you can avoid unnecessary recalculation. Right now you're running filter and map every time.

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠ Cautions

🚨 Solid.js Practical Cautions
  • Be careful with destructuring assignment: Destructuring Signals or props breaks reactivity. Instead of const { name } = props, access it as props.name, or use the splitProps() utility.
  • JSX differs from React: Solid.js's JSX is transformed into actual DOM manipulation code at compile time. Use class instead of className, and for event handlers, onclick (lowercase) is also supported in place of onClick (camelCase). style must be passed as an object.
  • Components run only once: Unlike React, the entire component function is not re-executed when state changes. The logic in the component body runs only once at mount, so place conditional logic inside JSX or within createEffect.
  • Ecosystem size: Compared to React, third-party libraries are limited. For things like complex form management and animation libraries, you may have to implement them yourself or wrap Vanilla JS libraries.
  • Conditional rendering approach: Using the <Show> component is recommended instead of the ternary operator {condition ? <A/> : <B/>}. The ternary operator may evaluate both sides, which can lead to unintended behavior.

πŸ”— Related Terms

πŸ“š Learn More