Introduction to the Strong JavaScript Library

No Comments

Strong is a reactive JavaScript library for creating person interfaces with out a digital DOM. It compiles templates all the way down to actual DOM nodes as soon as and wraps updates in fine-grained reactions in order that when state updates, solely the associated code runs.

This fashion, the compiler can optimize preliminary render and the runtime optimizes updates. This deal with efficiency makes it one of many top-rated JavaScript frameworks.

I acquired interested by it and needed to provide it a attempt, so I spent a while making a small to-do app to discover how this framework handles rendering elements, updating state, establishing shops, and extra.

Right here’s the ultimate demo for those who simply can’t wait to see the ultimate code and end result:

Getting began

Like most frameworks, we are able to begin by putting in the npm package deal. To make use of the framework with JSX, run:

npm set up solid-js babel-preset-solid

Then, we have to add babel-preset-solid to our Babel, webpack, or Rollup config file with:

“presets”: [“solid”]

Or for those who’d prefer to scaffold a small app, you too can use one in every of their templates:

# Create a small app from a Strong template
npx degit solidjs/templates/js my-app

# Change listing to the challenge created
cd my-app

# Set up dependencies
npm i # or yarn or pnpm

# Begin the dev server
npm run dev

There may be TypeScript assist so for those who’d like to start out a TypeScript challenge, change the primary command to npx degit solidjs/templates/ts my-app.

Creating and rendering elements

To render elements, the syntax is much like React.js, so it might sound acquainted:

import { render } from “solid-js/internet”;

const HelloMessage = props => <div>Hi there {props.title}</div>;

() => <HelloMessage title=”Taylor” />,

We have to begin by importing the render operate, then we create a div with some textual content and a prop, and we name render, passing the part and the container aspect.

This code then compiles all the way down to actual DOM expressions. For instance, the code pattern above, as soon as compiled by Strong, appears one thing like this:

import { render, template, insert, createComponent } from “solid-js/internet”;

const _tmpl$ = template(`<div>Hi there </div>`);

const HelloMessage = props => {
const _el$ = _tmpl$.cloneNode(true);
insert(_el$, () => props.title);
return _el$;

() => createComponent(HelloMessage, { title: “Taylor” }),

The Strong Playground is fairly cool and reveals that Strong has alternative ways to render, together with client-side, server-side, and client-side with hydration.

Monitoring altering values with Alerts

Strong makes use of a hook referred to as createSignal that returns two features: a getter and a setter. Should you’re used to utilizing a framework like React.js, this might sound a little bit bizarre. You’d usually anticipate the primary aspect to be the worth itself; nevertheless in Strong, we have to explicitly name the getter to intercept the place the worth is learn in an effort to observe its modifications.

For instance, if we’re writing the next code:

const [todos, addTodos] = createSignal([]);

Logging todos is not going to return the worth, however a operate as a substitute. If we need to use the worth, we have to name the operate, as in todos().

For a small todo record, this might be:

import { createSignal } from “solid-js”;

const TodoList = () => {
let enter;
const [todos, addTodos] = createSignal([]);

const addTodo = worth => {
return addTodos([…todos(), value]);

return (
<h1>To do record:</h1>
<label for=”todo-item”>Todo merchandise</label>
<enter sort=”textual content” ref={enter} title=”todo-item” id=”todo-item” />
<button onClick={() => addTodo(enter.worth)}>Add merchandise</button>
{todos().map(merchandise => (

The code pattern above would show a textual content discipline and, upon clicking the “Add merchandise” button, would replace the todos with the brand new merchandise and show it in an inventory.

This could appear fairly much like utilizing useState, so how is utilizing a getter completely different? Contemplate the next code pattern:

console.log(“Create Alerts”);
const [firstName, setFirstName] = createSignal(“Whitney”);
const [lastName, setLastName] = createSignal(“Houston”);
const [displayFullName, setDisplayFullName] = createSignal(true);

const displayName = createMemo(() => {
if (!displayFullName()) return firstName();
return `${firstName()} ${lastName()}`;

createEffect(() => console.log(“My title is”, displayName()));

console.log(“Set showFullName: false “);

console.log(“Change lastName “);

console.log(“Set showFullName: true “);

Operating the above code would lead to:

Create Alerts

My title is Whitney Houston

Set showFullName: false

My title is Whitney

Change lastName

Set showFullName: true

My title is Whitney Boop

The primary factor to note is how My title is … shouldn’t be logged after setting a brand new final title. It’s because at this level, nothing is listening to modifications on lastName(). The brand new worth of displayName() is just set when the worth of displayFullName() modifications, this is the reason we are able to see the brand new final title displayed when setShowFullName is about again to true.

This offers us a safer method to observe values updates.

Reactivity primitives

In that final code pattern, I launched createSignal, but additionally a few different primitives: createEffect and createMemo.


createEffect tracks dependencies and runs after every render the place a dependency has modified.

// Remember to import it first with ‘import { createEffect } from “solid-js”;’
const [count, setCount] = createSignal(0);

createEffect(() => {

Rely is at… logs each time the worth of depend() modifications.


createMemo creates a read-only sign that recalculates its worth at any time when the executed code’s dependencies replace. You’d use it once you need to cache some values and entry them with out re-evaluating them till a dependency modifications.

For instance, if we needed to show a counter 100 instances and replace the worth when clicking on a button, utilizing createMemo would enable the recalculation to occur solely as soon as per click on:

operate Counter() {
const [count, setCount] = createSignal(0);
// Calling `counter` with out wrapping it in `createMemo` would lead to calling it 100 instances.
// const counter = () => {
// return depend();
// }

// Calling `counter` wrapped in `createMemo` leads to calling it as soon as per replace.
// Remember to import it first with ‘import { createMemo } from “solid-js”;’
const counter = createMemo(() => {
return depend()

return (
<button onClick={() => setCount(depend() + 1)}>Rely: {depend()}</button>
<div>1. {counter()}</div>
<div>2. {counter()}</div>
<div>3. {counter()}</div>
<div>4. {counter()}</div>
<!– 96 extra instances –>

Lifecycle strategies

Strong exposes just a few lifecycle strategies, akin to onMount, onCleanup and onError. If we would like some code to run after the preliminary render, we have to use onMount:

// Remember to import it first with ‘import { onMount } from “solid-js”;’

onMount(() => {
console.log(“I mounted!”);

onCleanup is much like componentDidUnmount in React — it runs when there’s a recalculation of the reactive scope.

onError executes when there’s an error within the nearest little one’s scope. For instance we may use it when fetching information fails.


To create shops for information, Strong exposes createStore which return worth is a readonly proxy object and a setter operate.

For instance, if we modified our todo instance to make use of a retailer as a substitute of state, it might look one thing like this:

const [todos, addTodos] = createStore({ record: [] });

createEffect(() => {

onMount(() => {
addTodos(“record”, [
{ item: “a new todo item”, completed: false }

The code pattern above would begin by logging a proxy object with an empty array, adopted by a proxy object with an array containing the item {merchandise: “a brand new todo merchandise”, accomplished: false}.

One factor to notice is that the highest stage state object can’t be tracked with out accessing a property on it — this is the reason we’re logging todos.record and never todos.

If we solely logged todo` in createEffect, we might be seeing the preliminary worth of the record however not the one after the replace made in onMount.

To alter values in shops, we are able to replace them utilizing the setting operate we outline when utilizing createStore. For instance, if we needed to replace a todo record merchandise to “accomplished” we may replace the shop this manner:

const [todos, setTodos] = createStore({
record: [{ item: “new item”, completed: false }]

const markAsComplete = textual content => {
i => i.merchandise === textual content,
c => !c

return (
<button onClick={() => markAsComplete(“new merchandise”)}>Mark as full</button>

Management Stream

To keep away from wastefully recreating all of the DOM nodes on each replace when utilizing strategies like .map(), Strong lets us use template helpers.

Just a few of them can be found, akin to For to loop by way of objects, Present to conditionally present and conceal parts, Swap and Match to point out parts that match a sure situation, and extra!

Listed below are some examples exhibiting find out how to use them:

<For every={todos.record} fallback={<div>Loading…</div>}>
{(merchandise) => <div>{merchandise}</div>}

<Present when={todos.record[0].accomplished} fallback={<div>Loading…</div>}>
<div>1st merchandise accomplished</div>

<Swap fallback={<div>No objects</div>}>
<Match when={todos.record[0].accomplished}>
<CompletedList />
<Match when={!todos.record[0].accomplished}>
<TodosList />

Demo challenge

This was a fast introduction to the fundamentals of Strong. Should you’d prefer to mess around with it, I made a starter challenge you’ll be able to robotically deploy to Netlify and clone to your GitHub by clicking on the button under!

This challenge consists of the default setup for a Strong challenge, in addition to a pattern Todo app with the fundamental ideas I’ve talked about on this publish to get you going!

There may be way more to this framework than what I lined right here so be at liberty to test the docs for more information!

The publish Introduction to the Strong JavaScript Library appeared first on CSS-Tips. You’ll be able to assist CSS-Tips by being an MVP Supporter.

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment