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

@procore/react-state-patterns

v1.0.0

Published

Tiny utility package for easily creating reusable implementations of React state provider patterns.

Downloads

24

Maintainers

yzhou2024yzhou2024alyelashram_procorealyelashram_procoremelch-procoremelch-procorepeterknifpeterknifmoaz-ashrafmoaz-ashrafattachiattachia.elbadaweia.elbadaweihyogmanhyogmandmitri_wmdmitri_wmstephanie.breretonstephanie.breretonprocore-oss-userprocore-oss-userstevenkang3stevenkang3max.helmetagmax.helmetagcodyrobertsprocorecodyrobertsprocoremiguel.garcia-procoremiguel.garcia-procoremagdyyxmagdyyxatoaimaatoaimamustafa-abdelrahmanmustafa-abdelrahmanelewando-procoreelewando-procoreahmed.ghorabahmed.ghorablnspatz914lnspatz914richard.bunnrichard.bunnomar.wagdyomar.wagdymona.khairbekmona.khairbekmbartlett413mbartlett413cody_schindler_procorecody_schindler_procoreyoasyo25yoasyo25ritchleeritchleeandersontr15andersontr15steven.hinklesteven.hinklejamie-dugan-procorejamie-dugan-procorehgouhierprocorehgouhierprocoredenzylbalramdenzylbalramsarah.freitassarah.freitasalan.bresanialan.bresaniamyprocoreamyprocoreyoyis3000yoyis3000elijah.procoreelijah.procoremike-arndt-procoremike-arndt-procorejnhoang1jnhoang1pam-whisenhuntpam-whisenhuntshradha.khardshradha.kharddavid-christensen-procoredavid-christensen-procorejavio-procorejavio-procorechance.eakin.procorechance.eakin.procoregideon-procoregideon-procoreihor.diachenko_procoreihor.diachenko_procorejustinmwattsjustinmwattstedyangtedyangjyang-procorejyang-procorepwhisenhunt-procorepwhisenhunt-procorefairchildfairchildrodayna.ehabrodayna.ehabneil1023neil1023scottsternscottsternbrian.smith1brian.smith1g2mitchellg2mitchelldlameter-procoredlameter-procorekylepietzkylepietzabhijit-procoreabhijit-procorelhuang325lhuang325jake-pitkinjake-pitkinerikthoresonerikthoresonsimona.iancusimona.iancudecha-sansondecha-sansonaberkowitzaberkowitzasamayasamaymustafa-u-abdelrahmanmustafa-u-abdelrahmanrajatmenhdirattarajatmenhdirattajacksonleach-procorejacksonleach-procorepmfrawleypmfrawleyphunguyen-pcorphunguyen-pcortatsiana.cliftontatsiana.cliftondeiabdeiabsrichaitanya.peddintisrichaitanya.peddintikenny.foisykenny.foisymatheusprocorematheusprocorejgreene_procorejgreene_procorehectorthielehectorthieleetokarevetokarevdaniel.ferreira-contractordaniel.ferreira-contractordmccraw-procoredmccraw-procorecyrille.baicyrille.baigreg.sparksgreg.sparksfabiomelo513fabiomelo513phil.custerphil.custerbbreyel921bbreyel921amir-iskanderamir-iskanderneil.mckeemanneil.mckeemannickprocorenickprocorelzhou888lzhou888davidshuredavidshurestevenliprocorestevenliprocoreramysaid2ramysaid2refaiepcnrefaiepcnjgentesjgentesfaraz.haniffaraz.hanifmostafaeltazymostafaeltazyagamaleldinagamaleldinandrew.isaacandrew.isaacsaranahal2saranahal2rodrigo.dejuanarodrigo.dejuanakellen.stewartkellen.stewartbill-wagnerbill-wagnerezrasimeloffezrasimeloffjeffgiaquintojeffgiaquintogturkadzegturkadzesean.spearman.procoresean.spearman.procorekylemartinez-procorekylemartinez-procoreroobo-romeskiroobo-romeskiandres-mendez-procoreandres-mendez-procoregaurav.sharma.procoregaurav.sharma.procoretracy.ottotracy.ottosarah.herediasarah.herediavictorbendeck-pcvictorbendeck-pccbathgatecbathgatedavidkangprodavidkangprokyle.liukyle.liuamin.jaipuriamin.jaipurigrafffffffgrafffffffmishaelowoyemimishaelowoyemievan.cerwonka.procoreevan.cerwonka.procoreilya.dryha-contractorilya.dryha-contractorvaromirvaromiryogevfine1yogevfine1timofeeetimofeeematt.harris0223matt.harris0223winson.chuwinson.chuandersonbispoprocoreandersonbispoprocorescorgiat-procorescorgiat-procoreladavargaladavargaprocore_halzyprocore_halzyenyagaenyagawillpankonienwillpankoniensateesh-kadiyala-procoresateesh-kadiyala-procorechris.berberchris.berbertxin1txin1epalinprocoreepalinprocoremehrdad-panahandehmehrdad-panahandehtyler.wasden.procoretyler.wasden.procorejeremy.lundjeremy.lunddineshkumar.jayakdineshkumar.jayakryanfuentesprocoreryanfuentesprocorestajicsstajicsbrocktillotsonprocorebrocktillotsonprocorekyle.williamskyle.williamsdtorres-procoredtorres-procorenoor.alinoor.aliari-procoreari-procorealanprocorealanprocorejl4everjl4everjames.lawsonjames.lawsonajaykumar-procoreajaykumar-procoredennis.heckmandennis.heckmantara.chamberstara.chamberslalovar-procorelalovar-procorejames.clearyjames.clearychadryderchadryderdevin.cunningham.procoredevin.cunningham.procoreabhijit.patwardhanabhijit.patwardhanlydiaharalydiaharasherylnapigkitsherylnapigkitchangprocorechangprocoreapcarroll_procoreapcarroll_procoreandy.mayerandy.mayerbob.laskowskibob.laskowskivinaya-procorevinaya-procorekahliholmeskahliholmesandrew.wheelerandrew.wheelerleandro-procleandro-procyadhu.prakashyadhu.prakashjason-kayejason-kayejesse.olsenjesse.olsenpatrick.lardinpatrick.lardinbrad.uranibrad.uraniallenanle.procoreallenanle.procorebrookyboy009brookyboy009uddhavjoglekaruddhavjoglekardancingshelldancingshellrysmithprocorerysmithprocorerobbiegprocorerobbiegprocorejadamsssjadamsssjeremy.bouzigardjeremy.bouzigardtimdohertytimdohertyb.bookoutb.bookoutjalyngjalynghtaelhtaeldev-account-admindev-account-adminsseanwangsseanwangbhargavrndbhargavrndfarismmkfarismmkdannyporrellodannyporrellodanny.oudanny.oumessanjahmessanjaheyvettesoueyvettesoujgee67jgee67cagmzcagmzmariah_delaneymariah_delaneylukenispellukenispelkimhin267kimhin267juliana.hernandezjuliana.hernandezjudy-lu-pcjudy-lu-pcprocore-it-supportprocore-it-supportandrewburke-pcandrewburke-pcjkleintechjkleintechrachel.arkebauerrachel.arkebauerprocore-npm-botprocore-npm-botjames.dabbs-procorejames.dabbs-procorelaurenbrandsteinprocorelaurenbrandsteinprocorescottbieser-procorescottbieser-procorezach.mckenzie.procorezach.mckenzie.procoreshayonj_procoreshayonj_procoreheplayskeysheplayskeysmike.southmike.souththomasoboylethomasoboyledischordedischordederek-carter-procorederek-carter-procoredlgasserdlgassercfprocorecfprocoreevan.waitsevan.waitsjeremy-marcusjeremy-marcusjmejia-fsljmejia-fslersgonzaloersgonzalostephan-procorestephan-procorealeclarsenprocorealeclarsenprocoreyihai.zweifelyihai.zweifeljay-rajanjay-rajanjacky-leijacky-leipeter.jinpeter.jin

Readme

Logo

react-state-patterns

Tiny utility package for easily creating reusable implementations of React state provider patterns.

🚀 @procore/react-state-patterns makes it easy to (and reduces boilerplate) create implementations of common React state provider patterns.

⚠️ Powered by React Hooks under the hood. (This library has a peer dependency on react: >= 16.8.0)

Why?

react-state-patterns is incredibly simple, small, and lightweight — making it a great choice for applications that wish to keep their footprint small and do not want a complex state management solution. This library is also great for those that wish to utilize React Hooks for reusable state but do not want to couple their presentational components directly with state (this makes unit testing components much easier 🌟).

react-state-patterns also makes state management modular — increasing code clarity and making organization of state a breeze 🍃.

Getting Started

Install

npm install @procore/react-state-patterns --save

Usage

API

View API Docs Here

Creating State Patterns

Directly From Hook

import useProviders, { hookSchema } from '@procore/react-state-patterns';

// Create the state patterns
const Counter = useProviders(props => {
  const [count, setCount] = useState(props.initialValue || 0);
  const handlers = {
    incrementBy: value => setCount(count + value),
    decrementBy: value => setCount(count - value)
  };
  // hookSchema(...)
  //    => { counter: { state: { count: 0 }, handlers: { incrementBy: (v) => {...}, decrementBy: (v) => {...} } } }
  return hookSchema({ count: count }, handlers, "counter");
});

// Counter = { useHook, withState, State, Provider, Consumer }

Using useStateHook util

useStateHook API Docs

import useProviders, { useStateHook } from '@procore/react-state-patterns';

// Create the state patterns
const Counter = useProviders(
  useStateHook(
    (props) => ({ count: props.initialValue || 0 }),
    {
      incrementBy: state => value => ({ ...state, count: state.count + value }),
      decrementBy: state => value => ({ ...state, count: state.count - value })
    },
    "counter"
  )
);

// Counter = { useHook, withState, State, Provider, Consumer }

Use the patterns

Decorator Pattern

const Displayer = ({ counter: { state, handlers }}) => (
  <React.Fragment>
    <div>{state.count}</div>
    <button onClick={() => handlers.decrementBy(1)}>Decrement</button>
    <button onClick={() => handlers.incrementBy(1)}>Increment</button>
  </React.Fragment>
);

const StatefulDisplayer = Counter.withState(Displayer);

const rootElement = document.getElementById("root");
ReactDOM.render(<StatefulDisplayer initialValue={5} />, rootElement);

Edit React State Patterns (Decorator)

Render Prop Pattern

const Displayer = (props) => (
  <Counter.State initialValue={5}>
    {({ counter: { state, handlers } }) => (
      <React.Fragment>
        <div>{state.count}</div>
        <button onClick={() => handlers.decrementBy(1)}>Decrement</button>
        <button onClick={() => handlers.incrementBy(1)}>Increment</button>
      </React.Fragment>
    )}
  </Counter.State>
);

Edit React State Patterns (Render Prop)

Context Provider/Consumer Pattern

const Displayer = (props) => (
  <Counter.Provider initialValue={5}>
    <Counter.Consumer>
      {({ counter: { state, handlers } }) => (
        <React.Fragment>
          <div>{state.count}</div>
          <button onClick={() => handlers.decrementBy(1)}>Decrement</button>
          <button onClick={() => handlers.incrementBy(1)}>Increment</button>
        </React.Fragment>
      )}
    </Counter.Consumer>
  </Counter.Provider>
);

Edit React State Patterns (Provider/Consumer)

Custom Hook Pattern

const Displayer = (props) => {
  const { counter: { state, handlers } } = Counter.useHook({ initialValue: 5 });

  return (
    <React.Fragment>
      <div>{state.count}</div>
      <button onClick={() => handlers.decrementBy(1)}>Decrement</button>
      <button onClick={() => handlers.incrementBy(1)}>Increment</button>
    </React.Fragment>
  );
};

Edit React State Patterns (Custom Hook)

Code Style Guides

code style: prettier

Prettier is run as a pre-commit hook to automatically modify staged .js and .jsx files to adhere to base code style rules defined in the .prettierrc.

Eslint is also used as an in-editor linter, so be sure to install an appropriate Eslint Plugin for your editor of choice. Prettier rules are setup to take precedence and override any conflicting eslint rules.