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-light

v2.1.1

Published

Simplified redux without any boilerplate.

Downloads

14

Readme

redux-light

Simplified approach of using redux without any boilerplate - no action objects and reducers!

Based on single reducer that merges new state for each root property. Pseudocode is:

const newState = { ...oldState }
for (const rootProp in changes) {
    newState[rootProp] = { ...oldState[rootProp], ...changes[rootProp] }
}

Initial state has to be an object, and values of its root props too. So it is similar to combining reduces in vanilla redux where initial states are objects (and they usually are).

Table of contents

Install

npm install --save redux        // redux is a peer dependency
npm install --save redux-light

Initialize

import { createStore } from 'redux'
import { createReducer } from 'redux-light'

const reducer = createReducer({ initialState })
const store = createStore(reducer)

Example

It makes sense to import getState and setState directly if you don't create stores dynamically during runtime but create them only once on app start.

And yes, it can be tested by mocking imports.

store.ts

import { createStore } from 'redux'
import { createReducer, setStateAction, resetStateAction } from 'redux-light'

export type AppState = {
    auth: {
        loading: boolean
        token?: string
        error?: Error
    },
    settings: {
        theme: 'light' | 'dark'
    }
}

const initialState: AppState = {
    auth: {
        loading: false
    },
    settings: {
        theme: 'light'
    }
}

const reducer = createReducer({ initialState, validate: __DEV__ }) // __DEV__ is a react-native global

export const store = createStore(reducer)
export const getState = store.getState
export const setState = (state: StateChange<AppState>) => store.dispatch(setStateAction(state))
export const resetState = (state: StateChange<AppState>) => store.dispatch(resetStateAction(state))

actions/auth.ts

Just write all business logic as usual functions.

import { getState, setState } from '../redux/store';

export async const signIn = (options: { login: string, password: string }) => {
    if (getState().auth.loading) return;

    setState({ auth: { loading: true } });

    let token = undefined;
    try {
        token = await api.signIn(options);
    }
    catch (error) {
        setState({ auth: { loading: false, error } });
        return;
    }

    setState({ auth: { loading: false, token } });
}

react-redux

Use Provider, connect and useSelector same as before, except no need to use mapDispatchToProps or useDispatch.

views/SignIn.js

import { signIn } from '../actions/auth';

...

onSignInPress = () => {
    signIn({
        login: this.state.login,
        password: this.state.password
    });
}

...

export default connect((state: AppState) => ({
    loading: state.auth.loading,
    error: state.auth.error
}))(SignIn);

Additional logging

Trace argument can be used for additional logging of each action:

export const setState = (trace: string, state: StateChange<AppState>) => store.dispatch(setStateAction(state, trace))