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

redux-state-resolver

v0.4.1

Published

Component to resolve sequences of items in state before rendering

Downloads

8

Readme

Redux State Resolver

npm version license CircleCI

💡 Cleanly resolve a sequence of dependencies - write component logic that can assume the state has what it needs.

Description

ReduxStateResolver is a component that uses resolver objects to separate the checking of state dependencies from the components that use them, resulting in cleaner views and better code organisation.

It takes a list of resolver objects as a prop, and only renders the target component when all the resolvers pass. Otherwise an intermediate component associated with the missing dependency is rendered.

It works great with the routing layer, ensuring that view components can be written without a bunch of if statements checking for every permuation of missing state.

const loginResolver = {
  select: state => state.isLoggedIn,
  action: () => push('/login'),
  render: () => <div>Redirecting to login...</div>
};

const profileResolver = {
  select: state => state.user.profile,
  action: state => getProfile(state.user.id, state.authToken),
  render: () => <div>Loading profile...</div>
};

<ReduxStateResolver
  store={store}
  resolvers={[
    loginResolver,
    profileResolver
  ]}
>
  <ViewComponentUsingProfile/>
</ReduxStateResolver>

Installation

# with yarn
yarn add redux-state-resolver

# or with npm
npm i redux-state-resolver

API

Component

The component that is rendered when all dependencies are resolved can be specified multiple ways. Depending on your use case you may want to use one way or another.

Component prop

<ReduxStateResolver
  store={store}
  resolvers={resolverList}
  component={ViewComponentUsingProfile}
/>

Children

<ReduxStateResolver
  store={store}
  resolvers={resolverList}
>
  <ViewComponentUsingProfile/>
</ReduxStateResolver>

Child render prop

<ReduxStateResolver
  store={store}
  resolvers={resolverList}
>
  {(state) => {
    return <ViewComponentUsingProfile userProfile={state.profile} />
  }}
</ReduxStateResolver>

Render prop

<ReduxStateResolver
  store={store}
  resolvers={resolverList}
  render={(state) => {
    return <ViewComponentUsingProfile userProfile={state.profile} />
  }}
/>

Resolver object

A resolver is just a plain javascript object with a few properties:

{
  // The select function recieves state, and returns
  // false if the dependency still needs to be resolved
  select: state => state.user.profile,

  // The action function is called if the test function returns false.
  // It is passed state, and should return an action, which is dispatched.
  action: state => actionCreator(state.aThing),

  // Action creators are assumed to be asyncronously resolving - they do not
  // immediately update state. This is most often the case when the action starts
  // a http request.
  // If this is not the case, use a 'syncAction' creator instead.
  syncAction: state => actionCreator(state.aThing),

  // The component is a React component that is rendered while waiting for
  // the dependency to be resolved in state.
  // 'component' is aliased as 'intermediateComponent' as well.
  component: LoadingComponent,

  // You can use a render prop instead. The render function is passed the store
  // state as an argument
  render: (state) => {
    return <LoadingComponent someProp={state.somePartOfState} />
  },
}