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

@ryujaewan/restate

v1.0.8

Published

React State (`@ryujaewan/restate` is a lightweight library that simplifies state management in React applications. It enables easy sharing and updating of state across multiple components.)

Downloads

107

Readme

Restate Library Documentation

@ryujaewan/restate is a lightweight library that simplifies state management in React applications. It enables easy sharing and updating of state across multiple components.

Table of Contents

Installation

npm install @ryujaewan/restate

or

yarn add @ryujaewan/restate

Basic Usage

// state.ts
import { restate } from '@ryujaewan/restate';

export const useUser = restate({ id: 1, name: 'John Doe' });

// UserComponent.tsx
import React from 'react';
import { useUser } from './state';

function UserComponent() {
  const [user, setUser] = useUser();
  
  return (
    <div>
      <h1>User ID: {user.id}</h1>
      <p>Name: {user.name}</p>
      <button onClick={() => setUser({ ...user, id: user.id + 1 })}>
        Increment ID
      </button>
    </div>
  );
}

Advanced Features

Memoization

Restate allows you to optimize performance by memoizing state updates:

let [user, setUser] = useUser((prev, next) => {
  return prev.id % 2 === 0; // Component updates only when id is even
});

return (
  <h1 onClick={() => {
    setUser({
      id: Math.floor(Math.random() * 100),
    });
  }}>
    {user.id}
  </h1>
);

Update Options

You can control when updates occur using the updateOn option:

let [user, setUser] = useUser({ updateOn: 'topLevelChange' });

return (
  <h1 onClick={() => {
    setUser({
      id: Math.floor(Math.random() * 100),
    });
  }}>
    {user.id}
  </h1>
);

API Reference

restate(initialState)

Creates a new state hook.

  • initialState: The initial state object.

Returns: A custom hook for managing state.

useUser()

Custom hook created by restate.

Returns: An array containing the current state and a setter function.

useUser.get()

Retrieves the current state without subscribing to updates.

useUser.set(newState)

Updates the state and triggers re-renders in subscribed components.

  • newState: New state object or update function.

Examples

Multi-Component Updates

// ProfileComponent.tsx
import React from 'react';
import { useUser } from './state';

function ProfileComponent() {
  const [user] = useUser();
  return <p>Profile: {user.name}</p>;
}

// SettingsComponent.tsx
import React from 'react';
import { useUser } from './state';

function SettingsComponent() {
  const [user, setUser] = useUser();
  
  return (
    <button onClick={() => setUser({ ...user, name: 'Jane Doe' })}>
      Change Name
    </button>
  );
}

// App.tsx
import React from 'react';
import UserComponent from './UserComponent';
import ProfileComponent from './ProfileComponent';
import SettingsComponent from './SettingsComponent';

function App() {
  return (
    <div>
      <UserComponent />
      <ProfileComponent />
      <SettingsComponent />
    </div>
  );
}

In this example, when the name is changed in SettingsComponent, both UserComponent and ProfileComponent will automatically update to reflect the new name.

Using get() and set()

// NonReactiveComponent.tsx
import React from 'react';
import { useUser } from './state';

function NonReactiveComponent() {
  const handleClick = () => {
    const currentUser = useUser.get();  // This component won't re-render on state changes
    console.log("Current user:", currentUser);
    useUser.set(prevUser => ({ ...prevUser, name: 'Alice Johnson' }));
  };

  return <button onClick={handleClick}>Update User</button>;
}