Coder Social home page Coder Social logo

choo-redux's Introduction

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

choo-redux's People

Contributors

soyuka avatar

Stargazers

 avatar  avatar Piotr Synowiec avatar Thomas Colin avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.