Skip to content

Commit

Permalink
Add React support
Browse files Browse the repository at this point in the history
  • Loading branch information
kormanowsky committed Oct 17, 2020
1 parent 9e169bd commit 8660207
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,17 @@ let citiesNumber = citiesOfRussia.toNumber();
// Later, convert it back to object
let citiesFromNumber = new CitiesBitList(citiesNumber).toObject();
```

## New in v1.3.0: React support
The new `useBitList()` hook may be used in React applications.
```javascript
import BitList from "js-bit-list";
const MyBitList = BitList.useKeys(["myKey"]);
const [getList, withList] = useBitList(MyBitList, {myKey: 1});
// Later
const listNumber = getList().toNumber();
withList(list => {
list.setObject({myKey: 0})
});
```
### See example.js for working example
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,19 @@ if (typeof window === "object") {
window.BitList = BitList;
}

module.exports = BitList;
// ReactJS support

/**
* A React hook for bit list.
* @param {*} bitListClass A bit list class (child of BitList) to use
* @param {*} initialValue An initial value to use.
* @since 1.3.0
* @example const [getBitList, withBitList] = useBitList(MyBitList, {"mykey":true})
*/
function useBitList(bitListClass, initialValue){
let instance = new bitListClass(initialValue);
return [() => instance, callback => {callback(instance)}];
}

export {useBitList};
export default BitList;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-bit-list",
"version": "1.2.0",
"version": "1.3.0",
"description": "Store arrays of boolean values in numbers.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 8660207

Please sign in to comment.