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

ui-dimensions

v1.0.7

Published

Simple modular React ui state management

Downloads

1

Readme

UI Dimensions

Simple modular React UI state management for React

Summary

The state of state-management within the React ecosystem is vast and many good solutions have pushed our paradigms. Amongst these, it is hard to deny the impact that Redux has had, and how fundamentally, despite its faults, it has stood the test of time for its strengths.

Working with Redux can involve excessive boilerplate and an over-complication of simple work-flows. At the same time, ironically Redux or variations thereof have been popular choices for apps that must be able to scale.

Taking into consideration these observations, it's clear that the community has sought a happy medium where we can take advantage of the strengths of Redux while reducing or eliminating it's complicated usage.

Usage

Installation

npm install ui-dimensions

Quick Start Example

The following example demonstrates the primary simple use-case for ui-dimensions where we create a state dimension with createBaseDimension() and then use it in the following React code. This shows how ui-dimensions achieves the common functionality that Redux provides in a much more condensed and simple way.

An added feature here, however, is that by wrapping our initial state, and reducers within a set of closures, ui-dimensions is able to provide a set of given dependencies to each of these functions.

Although it is typically sufficient to use import statements in the code-base, there are certain cases where this form of dependency injection can by advantageous, and at a minimum can simplify testing while removing the need for extra import statements throughout different files.


const dimensionStoreKey = 'spaceVoyageManager'

const externalDependencies = {
    api: spaceVoyagerApi,
    translations: spaceVoyagerTranslations,
}

const initialStateClosure = ({ translations }) => {
    const initialState = {
        missionObjective: translations.discoverNewPlanets,
        discoveryCount: 0,
        voyages: [],
    }

    return initialState
}

const reducersClosure = ({ translations }) => (state) => {
    const reducers = {
        modifyMissionObjective: (missionObjective) => { ...state, missionObjective },
        setVoyages: (voyages) => { ...state, voyages },
        addVoyage: (voyage) => { 
            ...state, 
            discoveryCount: state.disoveryCount + (voyage.discoveryMade ? 1 : 0),
            voyages: [...state.voyages, voyage] 
        },
    }

    return reducers
}

// Create a dimension of state in our app to be used in UI
const dimension = createBaseDimension({
                        dimensionStoreKey,
                        initialStateClosure,
                        reducersClosure,
                        externalDependencies,
                    })

// Our React Component consuming the above created dimension
const SpaceVoyageManager = () => {
    const { 
        missionObjective,
        discoveryCount,
        voyages
    } = dimension.use()

    const addDiscoveryVoyage = () => {
        dimension.reducers.addVoyage({ discoveryMade: true })
    }

    return (
        <div>
            <h1>Space Voyage Manager of mission: {missionObjective}
            <h3>{discoveryCount} discoveries made on {voyages.length} voyages.<h3>
            <button type="submit">{addDiscoveryVoyage}</button>
        </div>
    )
}

From the above example, we have the following observations.

  1. createBaseDimension takes in:
  • A dimension store key acting as an identifier for the dimension in the overall store.
  • An external dependencies object.
  • Closures defining initial state and reducers with access to the passed in external dependencies object.
  1. We see that the returned dimension object contains:
  • A function called use which reactively selects state from our state store. This uses Redux' useSelector hook under the surface.
  • An object called reducers simply providing access to our defined reducers. The dispatch of actions is abstracted away here providing an explicit one to one relationship between reducers and actions. Pragmatically, this is almost always the desired case. Function composition can readily replace any cases where it is intended to have one 'action' trigger multiple reducer changes.

Full Usage

We can add more interesting functionality throught the use of createDimension

const asyncActionsClosure = ({ api, dimension }) => {
    const asyncActions = {
        getVoyages: () => api.getVoyages().then((voyages) => dimension.setVoyages(voyages)),
        addVoyage: (voyage) => api.addVoyage(voyage)
    }

    return asyncActions
}


const dimension = createDimension({
                                dimensionStoreKey,
                                initialStateClosure,
                                reducersClosure,
                                selectorsClosure,
                                asyncActionsClosure,
                                executorsClosure,
                                externalDependencies,
                            })