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

use-reactive-ts

v1.0.7

Published

[![npm](https://img.shields.io/npm/v/use-reactive-ts)](https://www.npmjs.com/package/use-reactive-ts) [![npm](https://img.shields.io/npm/dt/use-reactive-ts)](https://www.npmjs.com/package/use-reactive-ts) [![GitHub Workflow Status](https://img.shields.io/

Downloads

10

Readme

npm npm GitHub Workflow Status npm bundle size npm bundle size

Use Reactive TS

React hook for having a mutable state that will be reactive without using setState. this hook is provided with TS first approach and performance consideration so it will not increase the number of your components renders.

UseReactive Simple Usage


Motivation

React is a treble name for the React lib, as it doesn't react to changes, we tell React, listen we had a change, there is our new state, please change it. But this is not native. Take a minute to think about why counter++ is not enough to place the new value on the screen. Now try to think of some production complex state cases where you had to loop and find an object and only then do something to it while you have the object in your hand in the first place! Why not just change it.

This approach is probably not the React/Functional way of handling state changes, but hey, this is more programing language native. Take Immer for an example, how easy it is just to change the object you are running on.

A highly recommended session by Rich Harris about how React is not reactive by its nature and how that inspired him to write Svelt.


How it works

To have all React side effects by rerendering a component, useReactive uses useState and useMemo to improve memoization. The following is a sequence diagram which describes the flow of actions to have a reactive state.

UseReactive Simple Usage


Installation

npm install use-reactive-ts --save

Signature

function useReactive<T extends object>(initialState: T): T;

Usage

Simple Counter

type CounterState = {
    counter: number;
};

const Counter = () => {
    let state = useReactive<CounterState>({ counter: 0 });

    if (state.counter > 5) {
        state.counter = 0;
    }

    return (
        <div>
            <div>
                counter: {state.counter}
                <button onClick={() => state.counter++}>counter +</button>
            </div>
        </div>
    );
};

Counter with nested object

type CounterInnerState = {
    inner: {
        counter: number;
    };
};

const Counter = () => {
    let state = useReactive<CounterInnerState>({ inner: { counter: 0 } });

    if (state.inner.counter > 5) {
        state.inner.counter = 0;
    }

    return (
        <div>
            <div>
                counter: {state.inner.counter}
                <button onClick={() => state.inner.counter++}>counter +</button>
            </div>
        </div>
    );
};

For more usage, check out Test Suits and Examples.

Sandbox


Test

  • [x] Component is reactive by mutable state changes
  • [x] Number of component renders
  • [ ] Number of complex components tree renders
npm test