avatarSam Ho

Summary

The undefined website discusses the potential of Preact/Signals as a more intuitive and performant alternative to React's state management using hooks.

Abstract

Preact/Signals is gaining popularity as a state management library that promises to simplify frontend development by replacing React hooks with a more straightforward approach. This library offers optimal updates for better app performance and a reduced learning curve. The website provides code examples comparing traditional React state management with useState and useEffect to the Preact/Signals approach using signal and effect. It also demonstrates how Preact/Signals can eliminate the need for useContext by directly using signals for context sharing. The article encourages readers to explore Preact/Signals, offering links to GitHub, documentation, and code demos, and invites them to follow the author on Medium and LinkedIn for more insights.

Opinions

  • The author believes that Preact/Signals could potentially replace React hooks, suggesting a future where developers might not need useState.
  • The article expresses that Preact/Signals can lead to improved app performance due to its ability to handle optimal updates.
  • It is implied that Preact/Signals simplifies the learning process for new developers by reducing the complexity associated with React's hooks.
  • The author is optimistic about the benefits of Preact/Signals, encouraging the audience to invest time in learning about it.
  • By providing multiple code examples and demos, the author conveys a strong endorsement for Preact/Signals as a practical and beneficial tool for developers.
  • The call to action for clapping, following, and reaching out on social media suggests that the author values community engagement and feedback on their content.

Signals — Future of React State Management

Source: https://github.com/preactjs/signals

Preact/Signals is a library which is getting more popular. It shifts frontend development towards a more intuitive way of handling state.

This is a powerful library which could possibly replace React hooks (Imagine your code without useState!). It reduces your learning curve. ⏳ App performance could also be optimized, because they have a way to do optimal updates! ⚡

Therefore, it worth to invest some time looking into

👀 Github: https://github.com/preactjs/signals

👀 Documentation: https://preactjs.com/blog/introducing-signals/

Code example

To help you start, here is a brief introduction with some working code example.

❌ useState()

import { useState } from 'react';

function App() {
  const [count, setCount] = useState(0) /* useState to be removed */
  return (
    <>
      <h1>Counter</h1>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count}
        </button>
      </div>
    </>
  )
}

✅ useSignals()

import { signal } from '@preact/signals-react';
const count = signal(0); /* Declare a signal with initial value 0 */

function App() {
  /* No more useState */
  return (
    <>
      <h1>Counter</h1>
      <div className="card">
        {/* Update signal directly */}
        <button onClick={() => (count.value += 1)}>
          count is {count}
        </button>
      </div>
    </>
  );
}

Code Demo: https://stackblitz.com/edit/vitejs-vite-72syht?file=src%2FApp.tsx

❌ useEffect()

import { useState, useEffect } from 'react';

function App() {
  const [count, setCount] = useState(0);

  /* useEffect to be removed */
  useEffect(() => {
    console.log("count is updated to: " + count);
  }, [count]);

  return (
    <>
      <h1>Counter</h1>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count}
        </button>
      </div>
    </>
  );
}

export default App;

✅ effect()

import { signal, effect } from '@preact/signals-react';
const count = signal(0); 

function App() {

  /* effect, no dependancy array is needed */
  effect(() => {
    console.log("count is updated to: " + count);
  })

  return (
    <>
      <h1>Counter</h1>
      <div className="card">
        <button onClick={() => (count.value += 1)}>
          count is {count}
        </button>
      </div>
    </>
  );
}

Code Demo: https://stackblitz.com/edit/vitejs-vite-p2xqch?file=src%2FApp.tsx

❌ useContext()

import React, { useContext, useState } from 'react';

const Counter = React.createContext(0);

const Header = () => {
  /* useContext to be removed */
  const counter = useContext(Counter);
  return <div> Count: {counter} </div>;
};

function App() {
  const [count, setCount] = useState(0);
  return (
    <>
      {/* Context Provider to be removed */}
      <Counter.Provider value={count}>
        <Header />
        <h1>Counter</h1>
        <div className="card">
          <button onClick={() => setCount((prev: number) => prev + 1)}>
            Add Item
          </button>
        </div>
      </Counter.Provider>
    </>
  );
}

✅ Just use Signal! 😎

import { signal } from '@preact/signals-react';
const count = signal(0);

const Header = () => {
  return <div> Count: {count} </div>;
};

function App() {
  return (
    <>
      <Header />
      <h1>Counter</h1>
      <div className="card">
        <button onClick={() => count.value++}>Add Item</button>
      </div>
    </>
  );
}

Code Example: https://stackblitz.com/edit/vitejs-vite-15gnv5?file=src%2FApp.tsx

If you like this article, please give a clap 👏 and follow me on medium, so I could create more of these articles 🙂

I hope this article gives you some insight to start with. Happy coding! 👨‍💻

You can also reach me on Linkedin.

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

React
Reactjs
State Management
Preactjs
JavaScript
Recommended from ReadMedium