Socket
Socket
Sign inDemoInstall

react-use-object-state

Package Overview
Dependencies
3
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-use-object-state

React hooks to create stateful API's for reusable object patterns


Version published
Weekly downloads
271
increased by46.49%
Maintainers
1
Install size
111 kB
Created
Weekly downloads
 

Readme

Source

react-use-object-state

React hooks for managing and creating reusable stateful object patterns.

NPM JavaScript Style Guide Build Status

Demo

Edit react-use-object-state

Install

npm install --save react-use-object-state

or

yarn add react-use-object-state

Idea

How often do you find yourself creating a boolean useState flag? How often do you have a stateful array and implement the same manipulation methods (slice, push, pop, shift, etc) to modify the state? How about a generic counter state 1,2,3... What about pagination state, or even anchored elements state?

What about memoization with these stateful object patterns? If you want to employ memoization, every stateful object needs to have any or all state updater methods memoized. This can both muddy up your code with lots of useCallbacks and useMemos, and even introduce bugs with missing or constantly changing dependencies.

react-use-object-state solves all of the above problems with several common reusable stateful objects right out of the box, and with tools helpful in creating your own stateful object patterns where all methods are memoized.

State Hooks

useBooleanState

State for Booleans

Example
import React from "react";
import { useBooleanState } from "react-use-object-state";

const BooleanExample = () => {
  const lightSwitch = useBooleanState(false);

  return (
    <div>
      <button onClick={lightSwitch.setTrue}>Turn on</button>
      <button onClick={lightSwitch.setFalse}>Turn off</button>
      <button onClick={lightSwitch.toggle}>Toggle</button>

      <div>The light switch is turned {lightSwitch.state ? "on" : "off"}</div>
    </div>
  );
};

export default BooleanExample;
NameTypeDefaultDescription
stateBooleanState of the boolean object
setStateFunction(state: Boolean): voidSets the boolean state
setTrueFunction(): voidSets state to true
setFalseFunction(): voidSets state to false
toggleFunction(): voidToggles boolean state

useArrayState

State for Arrays

import React from "react";
import { useArrayState } from "react-use-object-state";

const mockData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const ArrayExample = () => {
  const list = useArrayState<number>(mockData);

  return (
    <div>
      <button onClick={list.clear}>Clear</button>
      <button onClick={() => list.push(list.state.length + 1)}>Push</button>
      <button onClick={list.pop}>Pop</button>
      <button onClick={list.reverse}>Reverse</button>

      {list.state.map((listItem) => (
        <div key={listItem}>{listItem} </div>
      ))}
    </div>
  );
};

export default ArrayExample;
NameTypeDefaultDescription
stateArrayState of the array object
setStateFunction(state: Array): voidSets the array state
clearFunction(): voidEmpty's the array ([])
reverseFunction(): voidReverses the array
popFunction(): voidPops value off of the end of the array (does nothing on empty array)
pushFunction(...vals: T[])Pushes values onto end of the array
shiftFunction(): voidRemoves value from beginning of array (does nothing on empty array)
unshiftFunction(...vals: T[])Pushes values onto beginning of array
insertAtFunction(val: T, index: number): voidInserts value at a given index (Does nothing out of bounds)
upsertAtFunction(val: T, index: number): voidRemoves value from beginning of array (Does nothing out of bounds)
deleteAtFunction(index: number): voidRemoves value from beginning of array (Does nothing out of bounds)

useUniqueArrayState

State for Unique Arrays (Sets)

NameTypeDefaultDescription
stateArrayState of the array object with unique vals
setStateFunction(state: Array): voidSets the array state with unique vals
clearFunction(): voidEmpty's the array ([])
reverseFunction(): voidReverses the array
toggleFunction(...vals: T[]): voidFor each val, either adds it to the array if it doesn't exist, or removes it if it already exists
popFunction(): voidPops value off of the end of the array (does nothing on empty array)
pushFunction(...vals: T[])Pushes unique values onto end of the array
shiftFunction(): voidRemoves value from beginning of array (does nothing on empty array)
unshiftFunction(...vals: T[])Pushes unique values onto beginning of array
insertAtFunction(val: T, index: number): voidInserts unique value at a given index (Does nothing out of bounds or for nonunique vals)
upsertAtFunction(val: T, index: number): voidRemoves value from beginning of array (Does nothing out of bounds or for nonunique vals)
deleteAtFunction(index: number): voidRemoves value from beginning of array (Does nothing out of bounds)

useMapState

State for Maps

import React from "react";
import { useMapState } from "react-use-object-state";

const CounterExample = () => {
  const map = useMapState();

  return (
    <div>
      <button onClick={map.clear} >
        Clear
      </Button>
      <button onClick={() => map.set('key', 'val')} >
        Set key val
      </Button>
      <button onClick={() => map.delete('key')} >
        Delete key val
      </Button>
      <h4>
        Value of key: {map.state.get('key')}
      </h4>
    </div>
  );
};
NameTypeDefaultDescription
stateMap<K,V>State of the Map object
setState(map: Map<K,V>) => voidSet the state of the Map Object.
clearVoidFunctionClears the Map state
setFunction(min: Number): voidSets key value pair for Map state
deleteFunction(max: Number): voidDeletes key value pair from Map state

useCounterState

State for Counters

import React from "react";
import { useCounterState } from "react-use-object-state";

const CounterExample = () => {
  const counter = useCounterState({ min: 0, max: 10, count: 0 });

  return (
    <div>
      <button onClick={counter.increment} >
        Increment
      </Button>
      <button onClick={counter.decrement} >
        Decrement
      </Button>

      <h4>
        Count: {counter.count}
      </h4>
    </div>
  );
};
NameTypeDefaultDescription
countNumberValue of the counter
minNumberMinimum possible value of the counter
maxNumberMaximum possible value of the counter
setCountFunction(count: Number): voidSets the counter count
setMinFunction(min: Number): voidSets the counter min
setMaxFunction(max: Number): voidSets the counter max
incrementFunction(): voidIncrement the count by 1 (won't go above max)
incrementByFunction(x: Number): voidIncrement the count by 'x' (won't go above max)
decrementFunction(): voidDecrement the count by 1 (won't go below min)
incrementByFunction(x: Number): voidDecrement the count by 'x' (won't go below min)

useAnchorElState

State for Anchored Elements (ie a button that opens a menu in its location)

import React from "react";
import { useAnchorElState } from "react-use-object-state";
import Button from "@material-ui/core/Button";
import Menu from "@material-ui/core/Menu";
import MenuItem from "@material-ui/core/MenuItem";

const AnchorElExample = () => {
  const { anchorEl, setAnchorEl, clearAnchorEl } = useAnchorElState(null);

  return (
    <div>
      <Button onClick={setAnchorEl}>Open Menu</Button>
      <Menu
        anchorEl={anchorEl}
        keepMounted
        open={Boolean(anchorEl)}
        onClose={clearAnchorEl}
      >
        <MenuItem onClick={clearAnchorEl}>Profile</MenuItem>
        <MenuItem onClick={clearAnchorEl}>My account</MenuItem>
        <MenuItem onClick={clearAnchorEl}>Logout</MenuItem>
      </Menu>
    </div>
  );
};
NameTypeDefaultDescription
anchorElReact.MouseEvent or nullAnchored element
setAnchorElFunction(element: React.MouseEvent or null): voidSets the anchored element
clearAnchorElFunction(): voidClears the anchored element (sets anchorEl state to null)
setStateFunction(state: {count: Number, min: Number, max: Number}): voidSets the counter state

usePaginationState

State for Pagination

import React from "react";
import { usePaginationState } from "react-use-object-state";

const PaginationExample = () => {
  const pagination = usePaginationState({ page: 0, rowsPerPage: 25 });

  return (
    <div>
      <button onClick={pagination.previousPage} >
        Prev
      </Button>
      <button onClick={pagination.nextPage} >
        Next
      </Button>

      <h4>
        Page: {pagination.page}
      </h4>
      <h4>
        Count: {pagination.rowsPerPage}
      </h4>
    </div>
  );
};
NameTypeDefaultDescription
pageNumberCurrent page number
rowsPerPageNumberNumber of rows to show per page.
order'asc', 'desc', undefinedOrder page data ascending or descending
orderByString, undefinedField, column, or key by which to order data
setPageFunction(page: Number): voidSets the page number
setRowsPerPageFunction(rowsPerPage: Number): voidSets the rows per page
sortFunction({order: 'asc' or 'desc' or undefined, orderBy: String or undefined): voidSorts the pagination ascending or descending by a field
nextPageFunction(): voidIncrements the page number by 1
previousPageFunction(): voidDecrements the page number by 1 (min 0)

Creating your own state objects

In addition to providing some common stateful object patterns out of the box, useObjectState can be used to build your own stateful object hooks. This library follows compositional factory patterns, where each stateful object has a factory describing the state interface. The useObjectState hook is the base of every state hook that takes a state factory as a first argument, and an initial state as a second argument.

useObjectState(<yourStateFactory>, <initialState>);

From there, it memoizes the state and state methods, and returns your state hook.

useObjectState example

Below is an example of how you would use useObjectState to create a boolean stateful object using JS. If you are using TS, here is the source code.

// this is how to create useBooleanState is created using JS
import { useObjectState } from "react-use-object-state";

export const booleanStateFactory = (setState) => ({
  setTrue: () => setState(true),
  setFalse: () => setState(false),
  toggle: () => setState((state) => !state),
});

export const useBooleanState = (initialState) =>
  useState(booleanStateFactory, initialState);

useState compositional architecture

For scalable architecture, react-use-object-state suggests using compositional factory patterns. This will help prevent architectural problems associated with classical inheritance, and will give you decoupled reusable factory methods.


// mammalMethods.js
const play = (state) => {...}
const walk = (state) => {...}
const run = (state) => {...}


// useCatState.js
import { useObjectState } from 'react-use-object-state';
import { play, walk, run } from './mammalMethods';

export const catStateFactory = (setState) => {
  return {

    // these are methods imported from mammalMethods.
    // setState will pass the state into each function
    play: () => setState(play),
    walk: () => setState(walk),
    run: () => setState(run),

    // these are specific cat methods
    meow: () => {...}
    takeBath: () => {...}
  };
};

export const useCatState = (initialState) => useObjectState(catStateFactory, initialState);

// useDogState.js
import { useObjectState } from 'react-use-object-state';
import { play, walk, run } from './mammalMethods';

export const dogStateFactory = (setState) => ({

  // these are methods imported from mammalMethods
  // setState will pass the state into each function
  play: () => setState(play),
  walk: () => setState(walk),
  run: () => setState(run),

  // these are specific dog methods
  bark: () => {...}
  wagTail: () => {...}
});

export const useDogState = (initialState) => useObjectState(dogStateFactory, initialState);

License

MIT © BenBrewerBowman

Keywords

FAQs

Last updated on 24 Apr 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc