Code Monkey home page Code Monkey logo

react-immutable-js-store's Introduction

react-immutable-js-store

React wrapper around immutable-js-store.

Installation

npm install react-immutable-js-store

Api

connect({ actions, component, store, subscriptions })

Connects a React component to an immutable-js-store store. This function accepts the following named parameters:

Property Type Required? Description
actions Object Expose updates to the store to the connected component
component Component โœ“ Decorated component to be connected to the store
store ImmutableStore โœ“ Store
subscriptions Object Expose the current state of the store to the connected component

The subscriptions object can contain 2 types of values:

  • Array values will be auto-subscribed and the corresponding property will be updated when the subscription is notified.
  • Keys with Function values will be called each time the store changes; a single parameter (the current state) will be passed.

Example

import React, { Component } from 'react'
import ImmutableStore from 'immutable-js-store'
import { connect } from 'react-immutable-js-store'

// Initialize store with default data
const store = new ImmutableStore({
  items: [],
  newItemText: ''
})

// Expose the current state of the store to the "connected" component.
// Keys with Array values will be subscribed and have their props auto-updated.
// Keys with Function values will be called each time the store changes.
const subscriptions = {
  hasNext: (state) => store.hasNext(),
  hasPrevious: (state) => store.hasPrevious(),
  newItemText: ['newItemText'],
  items: ['items']
}

// Expose updates to the store to the "connected" component
const actions = {
  addItem: (text) => {},
  removeItemAt: (index) => {},
  stepBack: () => {
    store.stepBack()
  },
  stepForward: () => {
    store.stepForward()
  },
  toggleAll: () => {},
  toggleItemAt: (index) => {}
}

// Dumb component to be connected to the ImmutableStore.
class Application extends Component {
  static propTypes = {
    addItem: PropTypes.func.isRequired,
    hasNext: PropTypes.bool.isRequired,
    hasPrevious: PropTypes.bool.isRequired,
    newItemText: PropTypes.string.isRequired,
    items: PropTypes.instanceOf(Immutable.List).isRequired,
    removeItemAt: PropTypes.func.isRequired,
    setNewItemText: PropTypes.func.isRequired,
    stepBack: PropTypes.func.isRequired,
    stepForward: PropTypes.func.isRequired,
    toggleAll: PropTypes.func.isRequired,
    toggleItemAt: PropTypes.func.isRequired
  };

  render () {
    // ...
  }
}

// Connect the 
export default connect({
  actions,
  subscriptions,
  store
})(Application)

react-immutable-js-store's People

Contributors

bvaughn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.