use-local-storage-set-state

1.3.1 • Public • Published

use-local-storage-set-state

This project has been deprecated in favor of using https://github.com/mcrowder65/mooks

This hook is available in that project.

NPM version Build Status Total Status PRs Welcome Coverage Status

Simple React.useState that sits on top of localStorage

Example:

import React from "react";
import useState from "use-local-storage-set-state";
 
function MyComponent() {
  const [value, setValue] = useState(
    false,
    "name-of-local-storage-key"
  );
 
  return <button onClick={() => setValue(!value)}>{String(value)}</button>;
}
 
export default MyComponent;

And on refreshes, the value will persist.

Usage in tests

Under the hood, this module is obviously using localStorage. When using this with jest, you will run into errors since jest does not have localStorage. Which is why this package requires jest-localstorage-mock as a peerDependency.

In order to get your tests to work, run npm install -D jest-localstorage-mock, and then in your jest configuration, add:

"setupFiles"[
  "jest-localstorage-mock"
]

Readme

Keywords

none

Package Sidebar

Install

npm i use-local-storage-set-state

Weekly Downloads

1

Version

1.3.1

License

ISC

Unpacked Size

11.4 kB

Total Files

11

Last publish

Collaborators

  • mcrowder65