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-object-hook

v1.0.6

Published

A dead simple react hook for complex multi-level state objects

Downloads

24

Readme

React-Object-Hook

A dead simple react hook for complex multi-level state objects

Installation

You know the drill, right?

npm i react-object-hook

or

yarn add react-object-hook

Usage

This thing's so simple your grandma could do it.

import useObject from 'react-object-hook';

const initialState = {
  a: 1,
  b: 2,
  c: 3,
  d: {
    foo: 'bar',
    zoo: 'boomafoo',
    thing: {
      a: 'majig',
    }
  }
}

const MyComponent = props => {
  const {state, setState, getState} = useObject(initialState);

  console.log(state); // logs initialState
  console.log(getState('d.thing.a')); // majig
  setStatePath('meme', 'd.thing.a'); 
  console.log(getState('d.thing.a')); // meme
  return (
    <div>Get the d: {getState('d.thing.a')}</div>
  )
}

API Reference

Maaaaa do I really gotta? UGH OK NO NEED TO NAG!

useObject(initialState) The initial state must be an object. GEE I WONDER WHY.

Returns an object with the following possible properties:

  • state: The object with the current value, just like any other react state in the history of react states.
  • setStatePath: A function with 2 arguments:
    • value: The value to set into the state. If there is no path, this should be an object and will override the entire stored object.
    • path: A path to the property to change. Standard lodash path. See Lodash Documentation for deets.
  • getState: A function that takes a path and returns the value at that path (instead of the whole object, for optimization).
    • path: See above, same deal
    • default: Value returned if the path leads to an undefined value. Just like lodash get().
  • setState: A function that takes in an object and merges it with the current object value, in a recursive fashion. This... is pretty much like the old setState's behaviour, sort of. Remember setState? Oh, but it doesn't provide the "old state", just use state for that!

The future is bleak and uncertain

I can't promise anything but I'm sure I'll update this with more features eventually. Feel free to suggest stuff.

And uhh... testing, I guess. Yeah, testing...

Contributing

I like sarcasm and stuff but I'm not protective of the stuff I do. PR this thing with non-shitty code and I'll definitely consider hitting that Merge button. Cuz you're awesome!