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

react-air-state

v0.1.4

Published

This library is designed to create a simple state outside react components. The state is reactive and can be obtained using the built-in `useValue` hook.

Downloads

288

Readme

React Air State

This library is designed to create a simple state outside react components. The state is reactive and can be obtained using the built-in useValue hook.

The library does not use the context and provider, so connecting and using it becomes very simple.

Warning! This library has not been used in a real project, use it at your own risk. I will be glad to receive feedback.

Installation

npm install react-air-state

Using

/* File counterState.ts */
import {airState} from "react-air-state"

export const counterState = airState(0)
/* File Counter.tsx */
import {counterState} from "./counterState"

const CounterComponent = () => {
    const counter = counterState.useValue()

    const increase = () => {
        counterState.dispatch(prevState => prevState + 1)
    }
    
    return (
        <div>
            <h1>{counter}</h1>
            
            <button onClick={increase}>Increase</button>
        </div>
    )
}

airState

Factory Function that creates an object with the required set of methods. It takes 1 argument, which is the default state value. If there is no default value, you must explicitly specify the future state type const valueState = airState<string>()

Methods

dispatch

This method allows you to update the state to a different value and start re-visualization. You can pass the new state directly or a function that calculates it from the previous state

useValue

The hook to extract the current state

useSelect

The hook that extracts the state calculated by the function (selector) passed to it

createSelector

Function for creating a typed selector

Example with useSelect and createSelector

import {airState} from "react-air-state"

export const userState = airState({
    name: "Smith",
    token: "token"
})

const selectToken = userState.createSelector((user) => user.token)

const Component = () => {
    const token = userState.useSelect(selectToken);
    
    /* ... */
}

subscribe

This method for passing your function to subscribe to a state change

getValue

This method allows you to get the current state

combineAirState

Factory Function for combining different states into one. It takes 2 arguments:

  • array of states
  • function that calculates a new state based on the transmitted states

Example

import {airState, combineAirState} from "react-air-state"

const postsState = airState([
    {
        id: 1,
        title: 'Post 1'
    },
    {
        id: 2,
        title: 'Post 2'
    }
])

const commentsState = airState([
    {
        id: 1,
        postId: 1,
        message: 'Comment 1'
    },
    {
        id: 2,
        postId: 1,
        message: 'Comment 2'
    }
])

const normalizePostsState = combineAirState([postsState, commentsState], (posts, comments) => {
    return posts.map(post => ({
        ...post,
        comments: comments.filter(comment => comment.postId === post.id)
    }))
})

const PostsComponent = () => {
    const posts = normalizePostsState.useValue()
    
    return (
        <div>
            {posts.map(post => (
                <div key={post.id}>
                    <h3>{post.title}</h3>
                    <p>Comment count: {post.comments.length}</p>
                </div>
            ))}
        </div>
    )
}

Methods

combineAirState creates an object with the same methods as airState, except for the methods dispatch, useSelect, createSelector