Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cycle-redux

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cycle-redux

Unofficial Redux bindings for Cycle.js.

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

cycle-redux

If you are familiar with Redux and Cycle.js, you already know how this works.

First we include the necessary modules.

import makeStateDriver from "cycle-redux"

import { run } from "@cycle/core"
import { div, button, p } from "@cycle/dom"


Then we create our reducer. This is a very simple one, but you could also combine multiple reducers using redux's combineReducers function.


// This is a reducer
function counter(state = 0, action) {

  switch(action.type) {
    case "INCREMENT":
      return state + 1
    case "DECREMENT":
      return state - 1
    default:
      return state
  }

}

Here we create our main method. Notice how each click-event maps to a state action, which changes the state, which then changes the DOM.


function main(sources) {

  // Create increment action stream.
  const increment$ = sources.DOM
    .select(".increment")
    .events("click")
    .map(e => ({type: "INCREMENT"}))

  // Create decrement action stream.
  const decrement$ = sources.DOM
    .select(".decrement")
    .events("click")
    .map(e => ({type: "DECREMENT"}))


  // Merge the two action streams.
  const state$ = increment$.merge(decrement$)


  // Create virtual DOM tree.
  const vtree$ = sources.state
    .map(count => (
      div([
        button(".decrement", "Decrement"),
        button(".increment", "Increment"),
        p("Counter: " + count)
      ])

    ))

  // Return virtual DOM and action stream
  return {
    DOM: vtree$,
    state: state$
  }


}

At last, we run our app.

The makeStateDriver function takes a reducer and an optional initial state. Here we start with 1.

run(main, {
  DOM: makeDOMDriver(document.getElementById("app")),
  state: makeStateDriver(counter, 1)
})

Keywords

FAQs

Package last updated on 16 Jan 2016

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc