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-use-level

v1.1.1

Published

Provider and hooks for interacting with leveldb in the browser

Downloads

4

Readme

react-use-level

A react provider and associated hooks for mutating and live querying into a leveldb. The example todo app shows all of the features. Use the various query types (key, keys, range) to pull data out of the local storage leveldb, and use the mutations (put, patch, del) to make changes to the data. Any change that has a query listening within its key range will re-fetch its data. This means your state is always up to date and reflecting the values of the local storage leveldb.

install

npm install react-use-level. This should be imported into a context where react already exists, as it is a peer-dependency.

api

LevelProvider, a react provider which should be the parent of any component that is looking to use the provided hooks.

useLevelKeyQuery, a react hook that is convinience around useLevelQuery(createKeyQuery(key, opts)). key is where the value is found, opts has the shape { map? : async (value, db) }.

useLevelKeysQuery, a react hook that is convinience around useLevelQuery(createKeysQuery(keys, opts)). keys is an array of where the values are found, opts has the shape { map : async (values, db)? }.

useLevelRangeQuery, a react hook that is convinience around useLevelQuery(createRangeQuery(opts)). opts has the shape { gt : key?, gte : key?, lt : key?, lte : key?, filter: async (value, values, db)?, map : async (value, values, db)?, limit: integer? }.

In the case where the query options accept both filter & map functions, filter runs before the map. The db passed into the map and filter function has the shape db : { get : async (key) => value, getMany : async (keys) => values, values : (rangeOpts) => iterator => yield value, keys : (rangeOpts) => iterator => yield key, iterator : (rangeOpts) => iterator => yield [key, value] }.

useLevelQuery, a react hook whose retun value is live state on a query into the level instance. Use createKeyQuery or createRangeQuery as the arguments to this hook.

createKeyQuery, a convinience function around creating a key query. Use its return value as the argument into useLevelQuery to have live state on that particular key.

createKeysQuery, a convinience function around creating a getMany keys query. Use its return value as the argument into useLevelQuery to have live state on that particular key.

createRangeQuery, a convinience function around creating a range query. Use its return value as the argument into useLevelQuery to have live state on a key range.

useLevelMutate, a react hook whose return value is two memoized functions, { put, patch, del }. put takes two arguments, (key, value), which are used to set a value in the underlying leveldb at defined key. patch also takes (key, value) but it will retrieve and merge the old value with the value being passed in. This is useful for partial updates to your values. del takes a single argument, (key), which is used to delete a value in the underlying levedb. All mutations will check for live queries in the range, and will re-run those queries if necessary. Since both of these functions are memoized and given access to the LevelContext values, be sure to list them as dependencies in any effect or callback that they are used within. This is shown in the example todo app with the const del = useCallback block.

LevelContext, a react context that provides direct access to all of the underlying state, and level instance. queries is a react state array that keeps track of all of the live queries in order to rerun them if a mutation occurs over its key range. dirtyQueries is a react state object whose keys are query.id and values are booleans denoting if the current query.id has had a mutation in its range and needs to be re-run. queryParams is a react state object whose keys are query.id and values are a stringified version of the query argument passed into the useLevelQuery hook. This is used to manage the query object within the queries array, ensuring the latest version of its props are being used for cache invalidations. This is more for internal use, but exposed in case you want to build on this layer of abstraction.