npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

signal-components

v0.12.0

Published

**Signal-components is the easy way to integrate real reactivity into your React app.**

Downloads

18

Readme

Signal-components is the easy way to integrate real reactivity into your React app.

Goals

  • Integration - Provide the simplest way to integrate any reactivity system into your React app.
  • Type Safety - Typescript is the first-class citizen in this project.

Documentation

Motivation

React is a great library for building user interfaces, but it lacks a built-in reactivity system. They chose to use rerenders as a way to update the UI, which is not always the best solution.

Signal-like reactivity systems are a great way to solve this problem. They don't trigger rerenders when the state changes, but instead, they trigger only the components that depend on the changed state. But this way is not ideal too. Any reactivity system designed for describing the signals globally, and it's hard to integrate them into existing React app.

Signal-components act as the glue between your hook-based components and the reactivity system.

Introduction

import {declareComponent} from 'signal-components';

type Props = {
  x: number;
  y: number;
};

const Component = declareComponent<Props>(({x, y}, options) => {
  //                                          ^
  //                                     Inside props
  // x and y are Atom<number> here
  const z = atom(ctx => ctx.spy(x) + ctx.spy(y));
  // init phase

  return ({spy}) => {
    // render phase
    return <div>{spy(z)}</div>
  }
});

<Component x={1} y={atom(1)} /> // <- Outside props

Phases

  • Init phase - This phase is called only once when the component is created. It's a good place to create atoms and functions.
  • Render phase - This phase is called every time the component is rendered. We delegate the rendering to React. It means you can use any React hooks and components inside the render phase.

Init Phase

  • global function wireHook(callback: () => T): Atom<T>. You can execute any React hooks code inside the callback. It is the good way to move to the reactive code.

Render Phase

You have access to the functions in the render phase:

  • spy - This function is used to subscribe rerender to a received Atom. If value inside the Atom is changed, the React render call.
  • component - This function is the way to create a new component for a received Atom. If value inside the Atom is changed, the React rerender only created component. Nothing more. It is the good way to minimize the rerenders.
  • reatomCtx - reatom context to call your atoms and actions if needed

Props

  • Outside props - The public API of the component. You can pass T | Atom here. Signal-components will pass it to the init phase as an Atom.
  • Inside props - The internal API of the component. You always get Atom here inside props. Nothing will distract you from your reactive code.
  • Props - The types. Please, don't use Atom here. Only raw types.

Stable functions

Props with prefix on[A-Z] are called "Stable functions" in signal-components. That means you receive the same function instance from the insideProps. Doesn't matter how many times the component is rendered and what you pass to the prop: different functions or the undefined value.

Stable functions benefits demo: https://codesandbox.io/p/sandbox/signal-components-stable-functions-7d4m34

Insights

Inside props is the Proxy object. That's the reason because we have one significant limitation: you cannot get rest of the inside props.

const Component = declareComponent<Props>((insideProps, options) => {
  const {...rest} = insideProps; // <- Error here. Use `rest` operator instead
  // ...
});

Operators

defaults

Defaults operator is a way to provide default values for the internal props.

const Component = declareComponent<Props>((insideProps, options) => {
  const {x = atom(1)} = insideProps; // <- Error here because x is Atom<undefined>. Not undefined
  const {x} = defaults(insideProps, {x: 1}); // <- Ok way
  // ...
});

rest

Rest operator is a way to get the rest of the internal props. See Insights for more information.

const Component = declareComponent<Props>((insideProps, options) => {
  const {x, ...rest} = insideProps; // <- Error here. Use `rest` operator instead
  
  const {x} = insideProps; 
  const restProps = rest(insideProps, ['x']); // <- Ok way
  // ...
});

getAllPropsSignal

getAllPropsSignal is a way to get all props as a signal. It's useful when you start to rewrite your component.

const Component = declareComponent<Props>((insideProps, options) => {
  return ({spy}) => {
    const unwrappedProps = spy(getAllPropsSignal(insideProps));
    //...
  };
});

We recommend don't use this operator in your code, but it is useful for refactoring purposes

Supported Reactivity Systems