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

@hlhr202/redox

v0.1.7

Published

[![npm](https://img.shields.io/npm/v/@hlhr202/redox.svg)](https://www.npmjs.com/package/@hlhr202/redox)

Downloads

10

Readme

Redox

npm

Redox is a Redux like React state management library which simulate the functionality of Redux dispatcher but maintaining the state with its auto data binder.

Usage

You must enable transform decorator in babel or set 'experimentalDecorators' true in tsconfig for the usage of connect decorator

connect accept an option object as following format

{
    // `modal` is the modal name, should be unique in `Provider` scope.
    modal: 'modalName',

    // `initialState` is the initial state of connected component.
    // It will be passed as props.
    // Can either be native js object, Immutable.js object, or a function which transform its props to an object.
    initialState: {
        yourStateKey: yourStateValue | (props) => yourStateValue
    },

    // `reducer` is the state transform functions with return of new state
    reducer: {
        reducerName: (prevState, action) => nextState
    },

    // `effects` include the side effects which handle complex asynchronouse procedure
    // It should return promise of action
    effects: {
        functionName: async() => action
    }
}

Example 1 - Simple App

import React from 'react'
import ReactDOM from 'react-dom'
import {connect, Provider} from '@hlhr202/redox'
import {Map} from 'immutable'

@connect({
    modal: 'count',
    initialState: Map({
        count: 0
    }),
    reducer: {
        inc: (state, action) => state.update('count', count => count + 1),
        dec: (state, action) => state.update('count', count => count - 1)
    }
})
class Counter extends React.PureComponent {
    handleClick = type => {
        const action = {type, modal: 'count'}
        this.props.dispatch(action)
    }
    render() {
        return (
            <div>
                <button onClick={this.handleClick.bind(this, 'dec')}>-</button>
                {this.props.state.get('count')}
                <button onClick={this.handleClick.bind(this, 'inc')}>+</button>
            </div>
        )
    }
}

ReactDOM.render(
    <Provider>
        <Counter />
    </Provider>,
    document.getElementById('root')
)

Example 2 - Initialize state with props

import React from 'react'
import ReactDOM from 'react-dom'
import {connect, Provider} from '@hlhr202/redox'
import {Map} from 'immutable'

@connect({
    modal: 'App',
    initialState: ({display}) => Map({display}),
    reducer: {
        toggle: state => state.update('display', display => !display)
    }
})
class Toggle extends React.PureComponent {
    handleClick = () => this.props.dispatch({modal: 'App', type: 'toggle'})
    render() {
        return (
            <div>
                <button onClick={this.handleClick}>ToggleButton</button>
                {this.props.state.get('display') && <div>Toggled!</div>}
            </div>
        )
    }
}

ReactDOM.render(
    <Provider>
        <Toggle display={true} />
    </Provider>,
    document.getElementById('root')
)

Example 3 - Side effects

import React from 'react'
import ReactDOM from 'react-dom'
import {connect, Provider} from '@hlhr202/redox'
import {Map} from 'immutable'

@connect({
    modal: 'Article',
    initialState: Map({
        id: 0,
        body: '',
        title: ''
    }),
    reducer: {
        fetch: (state, action) => {
            const {payload: {body, id, title}} = action
            return state.set('id', id).set('body', body).set('title', title)
        }
    }
    effects: {
        fetchData: async() => {
            const myFetch = await fetch('https://jsonplaceholder.typicode.com/posts/1')
            const json = await myFetch.json()
            return {
                modal: 'Article',
                type: 'fetch',
                payload: json
            }
        }
    }
})
class Effect extends React.PureComponent {
    render() {
        return (
            <div>
                <button onClick={() => this.props.fetchData()}>Fetch</button>
                <section>
                    <div>title: {this.props.state.get('title')}</div>
                    <div>id: {this.props.state.get('id')}</div>
                    <div>body: {this.props.state.get('body')}</div>
                </section>
            </div>
        )
    }
}

ReactDOM.render(
    <Provider>
        <Effect/>
    </Provider>,
    document.getElementById('root')
)