New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

use-reactive-ts

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-reactive-ts

[![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/

  • 1.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

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

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

Keywords

FAQs

Package last updated on 27 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc