JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 16
  • Score
    100M100P100Q35301F
  • License MIT

Predictable state container for self-contained React components

Package Exports

  • codux

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (codux) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

codux

Predictable state container for self-contained React components

Features

  • 2kb! (minified)
  • No dependencies (other than React)
  • Avoid repetitive props and callbacks throughout deeply nested components
  • Improved performance via React.PureComponent
  • Redux inspired API

Demo

Table of Contents

Installation

$ yarn add codux

Inline Example

import React from 'react'
import { Provider, Connect } from 'codux'

export default const InlineDemo = (
  // A Provider is a new instance of state for all nodes inside it
  <Provider
    count: 0
  >
    // Use Connect to subscribe to values from the Provider state
    <Connect
      subscribe={state => ({
        count: state.count
      })}
    >
      {({
        count
      }) => (
        <span>{count}</span>
      )}
    </Connect>
    // Every Connected component can use the 'dispatch' prop
    // to update the Provider's store
    <Connect>
      {({
        dispatch
      }) => (
        <button
          // 'dispatch' takes a function that receives the
          // current provider state and returns a new one.
          onClick={() => dispatch(state => ({
            ...state,
            count: state.count + 1 // Immutable is best for performance :)
          }))}
        >
          Increment Count
        </button>
      )}
    </Connect>
  </Provider>
)

HOC Example

import React from 'react'
import { Provider, Connect } from 'codux'


const Count = ({ count }) => ({
  <span>{count}</span>
}
// Use Connect to subscribe to values from the Provider state
const ConnectedCount = Connect(state => ({
  count: state.count
}))(Count)

// Every Connected component can use the 'dispatch' prop
// to update the Provider's store
const Increment = ({ dispatch }) => ({
  <button
    // 'dispatch' takes a function that receives the
    // current provider state and returns a new one.
    onClick={() => dispatch(state => ({
      ...state,
      count: state.count + 1 // Immutable is best for performance :)
    }))}
  >
    Increment Count
  </button>
}
const ConnectedIncrement = Connect()(Increment)

const HocDemo = () => (
  <div>
    <ConnectedCount />
    <ConnectedIncrement />
  </div>
)

// A Provider is a new instance of state for all nodes inside it
const ProvidedHocDemo = Provider(HocDemo)

export default ProvidedHocDemo

Contributing

To suggest a feature, create an issue if it does not already exist. If you would like to help develop a suggested feature follow these steps:

  • Fork this repo
  • $ yarn
  • $ yarn run storybook
  • Implement your changes to files in the src/ directory
  • View changes as you code via our React Storybook localhost:8000
  • Make changes to stories in /stories, or create a new one if needed
  • Submit PR for review

Scripts

  • $ yarn run storybook Runs the storybook server
  • $ yarn run test Runs the test suite
  • $ yarn run prepublish Builds for NPM distribution
  • $ yarn run docs Builds the website/docs from the storybook for github pages