npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

linking

v0.1.0-beta.5

Published

Alternaive Redux bindings for React

Downloads

430

Readme

Linking

Alternative Redux bindings for React.

build status coverage npm dependencies

API

linkApp({reducers, [preload], component, [env]})

Create a store and mount the app with it.

Install peer react implementation (preact or react + react-dom) and import corresponding version(import {linkApp} from 'linking/preact' or import {linkApp} from 'linking/react') to use.

Coming s∞n: import linkApp from 'linking/vue'

Arguments

  • reducers (Object): An object that will be passed to combineReducers of Redux. In addition to reducing functions, object values can also be updater maps, and will be converted to reducing functions by handle-actions.
  • preload (Function): A function that returns preloaded state.
  • component ((p)React component): The root component of the app.

Returns

(Store): Redux store created with specified reducers.

While in development environment, HMR helper functions will also be added to the store object. replaceApp can be used to replace the root component, and replaceReducers also accepts the reducers object passed to linkApp.

Example

import linkApp from 'linking/preact'
import reducers from './reduce'
import preload from './preload'
import main from './main'

const store = linkApp({reducers, preload, component: main})

if (module.hot) {
  module.hot.accept('./reduce', () => store.replaceReducers(reducers))
  module.hot.accept('./main', () => store.replaceApp(main))
}

composeLink({Component, createClass, [propTypes]})

Create the link function for specified react implementation.

Examples

React

import createClass from 'create-react-class'
import propTypes from 'prop-types'
import composeLink from 'linking'

const link = composeLink({createClass, propTypes})

preact

import {Component} from 'preact'
import composeLink from 'linking'

const link = composeLink({Component})

link(render[, select][, merge])

Link a render function to a store. Return a linked render function or component. props.store will be used if store is passed to the returned function, other components use the store provided by their ancestor.

Arguments

  • select(state[, ownProps]): Select state properties related to the linked component. Called when store state changes, re-rendering is skipped if result of this function is shallowly equal to previous. Omit this argument to ignore store updates. Defaults to () => undefined.

  • merge(selectedState, bindAction, [ownProps]) Defaults to selectedProps => selectedProps. Compose the props object to be passed to the linked component.

    bindAction(action, [options]) Creates event handler functions that dispatch passed action. It is inversion of control to dispatch.

    action: object or createAction(event, [options]) action can also be a function returns an action, or a promise resolves to an action.

  • render(props): accepts a single "props" object argument with data and returns a React element.

Examples

Inject store for child elements
import createClass from 'create-react-class'
import propTypes from 'prop-types'
import composeLink from 'linking'

const link = composeLink({createClass, propTypes})
const root = link(renderRoot)
ReactDOM.render(root({store}), document.querySelector('#root'))
Inject todos and addTodo
function mapStateToProps(state) {
  return { todos: state.todos }
}

function merge(state, bindAction) {
  return Object.assign({onClick: bindAction({type: 'ADD_TODO'})}, state)
}

export default link(todoApp, mapStateToProps, merge)
implement connect and Provider of react-redux
import React from 'react'
import composeLink from 'linking'
const h = React.createElement
const link = composeLink({createClass, propTypes})

function Provider({store, children}) {
  const element = react.Children.only(children)
  function seed() {
    return element
  }
  const container = link(seed)
  return h(container, {store})
}

function defaultDispatch(dispatch) {
  return {dispatch}
}
function defaultMerge(state, actions, props) {
  return Object.assign({}, props, state, actions)
}
function mapDispatch(mapDispatchToProps, dispatch, ownProps) {
  if (typeof mapDispatchToProps == 'function') {
    return mapDispatchToProps(dispatch, ownProps)
  }
  return bindActionCreators(mapDispatchToProps, dispatch)
}

function connect(mapStateToProps,
  mapDispatchToProps=defaultDispatch,
  mergeProps=defaultMerge) {
  function merge(stateProps, {dispatch}, ownProps) {
    const actions = mapDispatch(mapDispatchToProps, dispatch, ownProps)
    return mergeProps(stateProps, actions, ownProps)
  }
  return Component => {
    const render = props => h(Component, props)
    link(render, mapStateToProps, merge)
  }
}

export {Provider, connect}

handleActions(updaterMap[, defaultState={}]): reduce

Turn an updater map into a reducing function.

The updater with key equal to type of dispatched action type is used for reduction. Current state and payload of the action is passed to the updater function, and result of it is shallowly merged into current state, like setState. If the updater returned a falsly value, or there's no updater for the action type, original state is returned instead.

Exmaples

const updaters = {
  increment: ({count}, payload) => ({count: count + payload}),
  decrement: ({count}, payload) => ({count: count - payload}),
  reset: (state, action) => ({count: 0})
}
const reduce = handleActions(updaters)
let actual, expected

actual = reduce({count: 1, data: 't'}, {type: 'increment', payload: 2})
expected = {count: 3, data: 't'}
t.deepEqual(actual, expected)

actual = reduce({count: 6}, {type: 'decrement', payload: 1})
expected = {count: 5}
t.deepEqual(actual, expected)

composeReduce(reducers)

Turns an object into a single reducing function like combineReducers.

Arguments

reducers (Object): In addition to reducing functions, object values can also be updater maps, and will be converted to reducing functions by handleActions.

Examples


function todos(state = [], action) {
  switch (action.type) {
    case 'ADD_TODO':
      return state.concat([action.text])
    default:
      return state
  }
}

const counter = {
  INCREMENT: ({counter}, {amount}) => ({counter: counter + amount}),
  DECREMENT: ({counter}, {amount}) => ({counter: counter - amount})
}

const reducer = composeReduce({todos, counter})
const store = createStore(reducer)

sideEffect :: Component -> handleChange -> Component

Create a component that, when mounting, updating, unmounting, calls handleChange with props of each mounted instance.

Examples

Setting document title

import {Component} from 'react'
import {sideEffect} from 'linking'

const arrayLast = list => list[list.length -1]
const setTitle = propsList => {
  const {title=document.title} = arrayLast(propsList)
  document.title = title
}

export default sideEffect(Component)(setTitle)