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-local-storage-context

v1.0.1

Published

A React context provider for managing local storage

Downloads

16

Readme

react-local-storage-context

A custom React hook and context provider to easily manage localStorage with React context. This package provides a simple API to store and retrieve values from localStorage while keeping them in sync with your React app's state using context and hooks.

Installation

To install the package, run:

npm install react-local-storage-context

Usage
1. Wrap Your App with StorageProvider
First, wrap your app with the StorageProvider component to provide the localStorage context to all child components.

tsx
Copy code
import React from 'react';
import { StorageProvider } from 'react-local-storage-context';
const App = () => {
  return (
    <div>
      <h1>My React App</h1>
    </div>
  );
};

const Root = () => (
  <StorageProvider>
    <App />
  </StorageProvider>
);

export default Root;
2. Use useStorage Hook in Your Components
Now, you can use the useStorage hook to access and update localStorage values. The hook returns the current value from localStorage and a setValue function to update it.

Basic Example
import React from 'react';
import { useStorage } from 'react-local-storage-context';

const UserNameComponent = () => {
  const { value, setValue } = useStorage<{ name: string }>();

  return (
    <div>
      <h1>Welcome {value?.name || 'Guest'}</h1>
      <button onClick={() => setValue({ name: 'Megha' })}>Set Name</button>
    </div>
  );
};

export default UserNameComponent;
In the above example:

The useStorage hook accesses the localStorage for a name property.
The setValue function is used to set a new value for name in localStorage.
3. Optional: Set Default Values
You can provide default values for localStorage when using the useStorage hook. This is useful if you want to initialize values in localStorage.
const { value, setValue } = useStorage<{ firstName: string; lastName: string }>({
  firstName: 'John',
  lastName: 'Doe',
});
In this example, calling clearName will remove the name value from localStorage.

API
StorageProvider: A component that provides the localStorage context to the app.
useStorage: A hook to get and set values in localStorage.
Arguments for useStorage
type (optional): You can provide an object type to define the structure of the value you want to store in localStorage.
Methods
value: The current value stored in localStorage.
setValue: A function to set or update the value in localStorage.
clearValue: A function to clear the value in localStorage.
Example Project
Here's a simple example project that uses react-local-storage-context:

tsx
Copy code
import React from 'react';
import { StorageProvider, useStorage } from 'react-local-storage-context';

const App = () => {
  const { value, setValue } = useStorage<{ name: string }>();

  return (
    <div>
      <h1>Welcome {value?.name || 'Guest'}</h1>
      <button onClick={() => setValue({ name: 'Megha' })}>Set Name</button>
    </div>
  );
};

const Root = () => (
  <StorageProvider>
    <App />
  </StorageProvider>
);

export default Root;
License
MIT License. See LICENSE for more information.

Contributing
If you find a bug or want to contribute to the project, feel free to open an issue or submit a pull request.

markdown
Copy code

### Notes on the `README.md` file:
- **Installation**: Instructions for installing the package via npm.
- **Usage**: Basic and optional usage examples for both basic and advanced implementations.
- **API**: A simple overview of the API (methods and properties available via the hook).
- **Example Project**: A complete example React app using the package.
- **License and Contributing**: Details on how to contribute and the project's license.