Skip to content

Basic wrapper for file storage strongly inspired by react hooks.

License

Notifications You must be signed in to change notification settings

Aliath/use-fs-storage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

test coverage Coverage Status npm bundle size npm

use-fs-storage

Basic wrapper for file storage strongly inspired by react hooks. It allows us to storage variable passed through reference (such as object, array and other possible JSON values). Make react-like data flow with no effort!

Installation

yarn add use-fs-storage

or

npm i --save use-fs-storage

Usage

import useStorage from 'use-fs-storage';


// first use
(async () => {
  const [users, setUsers] = await useStorage('users.json', {
    defaultValue: [],
    immediatelySync: true, // sync right after create store
    overrideDefault: true, // if specified path exists it will override default value
  });

  console.log(users); // []
  await setUsers([1, 2, 3]); // save value of "users" variable to "users.json"
  console.log(users); // [1, 2, 3]
  await setUsers(previousValue => previousValue.map(item => item * 2));
  console.log(users); /// [2, 4, 6] - same as "users.json" value
})();


// second use
(async () => {
  const [users, setUsers] = await useStorage('users.json', {
    defaultValue: [],
    immediatelySync: true,
    overrideDefault: true,
  });

  console.log(users); // [2, 4, 6]
  await setUsers(previousValue => previousValue.map(item => item * 2));
  console.log(users); /// [4, 8, 12] - same as "users.json" value
})();

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

About

Basic wrapper for file storage strongly inspired by react hooks.

Topics

Resources

License

Stars

Watchers

Forks