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

nanoweb-react

v1.0.2

Published

A utility library for React applications.

Downloads

114

Readme

Here is a detailed README.md for your nano-react library:

Nano React

nano-react is a lightweight utility library for React applications that provides custom hooks to persist state in various web storage mechanisms: localStorage, sessionStorage, and IndexedDB. This library makes it easy to manage state with persistence, ensuring your data is retained even after a page reload.

Table of Contents

Installation

You can install nano-react via npm:

npm install nano-react

or using Yarn:

yarn add nano-react

Usage

Importing the Hooks

import { useLSState, useSSState, useIDbState } from 'nano-react';

useLSState (localStorage)

useLSState is a custom hook that manages state with persistence in localStorage.

Parameters

  • name (string): The key under which the state will be stored in localStorage.
  • defaultValue: The initial value if nothing is found in localStorage.

Example

const [count, setCount] = useLSState('app.count', 0);

<button onClick={() => setCount(count + 1)}>
  localStorage count: {count}
</button>

useSSState (sessionStorage)

useSSState is a custom hook that manages state with persistence in sessionStorage.

Parameters

  • name (string): The key under which the state will be stored in sessionStorage.
  • defaultValue: The initial value if nothing is found in sessionStorage.

Example

const [sessionCount, setSessionCount] = useSSState('app.sessionCount', 0);

<button onClick={() => setSessionCount(sessionCount + 1)}>
  sessionStorage count: {sessionCount}
</button>

useIDbState (IndexedDB)

useIDbState is a custom hook that manages state with persistence in IndexedDB. It allows more complex data storage and is suitable for larger datasets compared to localStorage or sessionStorage.

Parameters

  • key (string): A unique key to identify the state in IndexedDB.
  • defaultValue: The initial value if nothing is found in IndexedDB.
  • dbOpts (object): Options for the database:
  • name (string): The name of the IndexedDB database. Default is 'db'.
  • store (string): The name of the object store within the database. Default is 'store'.

Example

const [dbCount, setDbCount] = useIDbState('app.dbCount', 0, {
  name: 'userDatabase',
  store: 'userData',
});

<button onClick={() => setDbCount(dbCount + 1)}>
  IndexedDB count: {dbCount}
</button>

Examples

Here is a full example demonstrating all hooks in a simple React component:

import React from 'react';
import { useLSState, useSSState, useIDbState } from 'nano-react';

function App() {
  const [count, setCount] = useLSState('app.count', 0);
  const [sessionCount, setSessionCount] = useSSState('app.sessionCount', 0);
  const [dbCount, setDbCount] = useIDbState('app.dbCount', 0, {
    name: 'userDatabase',
    store: 'userData',
  });

  return (
    <div>
      <button onClick={() => setCount(count + 1)}>localStorage count: {count}</button>
      <button onClick={() => setSessionCount(sessionCount + 1)}>sessionStorage count: {sessionCount}</button>
      <button onClick={() => setDbCount(dbCount + 1)}>IndexedDB count: {dbCount}</button>
    </div>
  );
}

export default App;

API Reference

useLSState(name, defaultValue)

  • Description: Manages state in localStorage.
  • Parameters:
  • name (string)
  • defaultValue

useSSState(name, defaultValue)

  • Description: Manages state in sessionStorage.
  • Parameters:
  • name (string)
  • defaultValue

useIDbState(key, defaultValue, dbOpts)

  • Description: Manages state in IndexedDB.
  • Parameters:
  • key (string)
  • defaultValue
  • dbOpts (object) => { name, store }

Contributing

Contributions are welcome! Please read the CONTRIBUTING.md file for guidelines on contributing to this project.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Notes:

  • Installation Instructions: Describes how to install the package.
  • Usage Examples: Provides clear examples for each hook.
  • API Reference: Detailed explanation of each hook and its parameters.
  • Contributing and License: Information on how to contribute and the project’s license.

Feel free to modify the content to better suit your needs and add any additional sections as necessary, such as FAQs or a section on common issues.