Socket
Socket
Sign inDemoInstall

choo-redux

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    choo-redux

Redux bridge for choojs.


Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Install size
4.67 kB
Created
Weekly downloads
 

Readme

Source

choo-redux

Redux bridge for choojs.

Disclaimer

  1. You Might Not Need Redux
  2. Choojs already has a state system similar to redux (see choo.use and the emitter)
  3. I made this because I want to benefit from the redux ecosystem with choo but in most cases this is not needed!

Install

npm install choo-redux -S

Usage

const { createStore, applyMiddleware } = require('redux')
const { patchRouter, chooMiddleware, changeDOMTitle } = require('choo-redux')
const choo = require('choo')
const html = require('choo/html')

const reducer = function (state = {}, action) {
  switch (action.type) {
    default:
      return state
  }
}

const app = choo()
const store = createStore(reducer, applyMiddleware(chooMiddleware(app)))

patchRouter(app, store)

app.route('/', mainView)

function mainView (state, dispatch) {
  if (state.title !== TITLE) dispatch(changeDOMTitle(🚂🚋🚋))

  return html`
    <body>
      <h1>Choo choo!</h1>
    </body>
  `
}

Under the hood

chooMiddleware

It propagates native choo events (eg: render, 'DOMTitleChange' etc.) from redux through nanobus (the choo event emitter).

This means that dispatching a render action will call emit('render') in choo:

const {render} = require('choo-redux')

// somewhere in the code, trigger choo rendering:
dispatch(render())

If you want to render as side-effect on an action, use {render: true} in the action:

function customAction(payload = {}) {
  return {type: CUSTOM_ACTION, render: true, payload}
}

dispatch(customAction)

patchRouter

The patchRouter allows to use redux state and the dispatch method inside views.

Indeed, the view now gets store.getState() and dispatch instead of the initial state and emit arguments:

/**
 * Note that the state is actually:
 * Object.assign({}, state, store.getState())
 */
app.route('/', function mainView (state, dispatch) {
  // dispatch a redux action:
  dispatch(Action({payload: 'foo'}))
})

API

The following action creators are available:

changeDOMTitle(title: string): {type: CHANGEDOMTITLE, payload: string}
render(): {type: RENDER}
pushState(route: string): {type: PUSHSTATE, payload: string}
replaceState(route: string): {type: REPLACESTATE, payload: string}
popState(route: string): {type: POPSTATE, payload: string}

Every choo native events have associated types which are exported as:

const {types} = require('choo-redux').types

FAQs

Last updated on 18 Oct 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc