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

@surglogs/with-partial-action-bind

v1.1.0

Published

withPartialActionBind HOC for connecting a partially applying Redux actions

Downloads

526

Readme

withPartialActionBind

Build Status

withPartialActionBind is a Higher Order Component that allows you to connect a Redux action and bind some of action's arguments with incoming props or redux state.

Instalation

npm i @surglogs/with-partial-action-bind recompose

What is this library good for?

It helps you to cut the boilerplate that is often needed when dispatching a Redux action.

Todo list example

Let's say we have a list of todos stored in the redux store. User did some changes to them and we have to send the edited todos to our server. Therefore we need to get the todos from our state and dispatch updateTodos action. We can do these two things nicely at once using withPartialActionBind:

import withPartialActionBind from '@surglogs/with-partial-action-bind'

// We will not go into the details how the the todos are actually updated
// We will simply assume there is some Redux middleware for handling async workflow like redux thunk, redux saga or redux promise middleware which sends them to the server
const updateTodos = (todos) => {
  return {
    type: 'UPDATE_TODOS',
    payload: {
      todos
    }
  }
}

const SaveTodosButton = ({ updateTodos }) => <button onClick={updateTodos}>Save todos</button>

const ConnectedSaveTodosButton = withPartialActionBind({
  action: updateTodos,
  name: 'updateTodos',
  args: [state => state.todos]
})(SaveTodosButton)

export default ConnectedSaveTodosButton

That's it! In args: [state => state.todos] we binded the todos argument to be state.todos, therefore we can directly pass the binded action updateTodos to the button as an onClick handler.

Let's say now that we have multiple todo lists, therefore if we want to update todos of one todo list we also need to send the name of the todo list. The name is passed as a prop name to the ConnectedSaveTodosButton component. Our code will look like this:

const updateTodos = (todos, todoListName) => {
  return {
    type: 'UPDATE_TODOS',
    payload: {
      todos,
      todoListName
    }
  }
}

const SaveTodosButton = ({ updateTodos }) => <button onClick={updateTodos}>Save todos</button>

const ConnectedSaveTodosButton = withPartialActionBind({
  action: updateTodos,
  name: 'updateTodos',
  args: [state => state.todos, 'name']
  // args: [state => state.todos, (state, props) => props.name] // also works :)
})(SaveTodosButton)

As you can see, we changed our args to [state => state.todos, 'name']. By putting 'name' we are telling that the second argument should be an incoming prop name.

We will change our component again. Let's say for some reason we do not want to bind the todoListName argument. That is also possible:

const SaveTodosButton = ({ updateTodos }) => <button onClick={() => updateTodos('our great todoList')}>Save todos</button>

const ConnectedSaveTodosButton = withPartialActionBind({
  action: updateTodos,
  name: 'updateTodos',
  args: [state => state.todos]
})(SaveTodosButton)

If we do not bind all of the arguments in withPartialActionBind, we can just pass them when calling the action.

Finally, what if we don't want to bind the todoListName again but it is the first argument of the action? Like this:

const updateTodos = (todoListName, todos) => {
  return {
    type: 'UPDATE_TODOS',
    payload: {
      todos,
      todoListName
    }
  }
}

We can pass a special function builder, that allows us to pass the argument using custom logic:

const SaveTodosButton = ({ updateTodos }) => <button onClick={() => updateTodos('our great todoList')}>Save todos</button>

const ConnectedSaveTodosButton = withPartialActionBind({
  action: updateTodos,
  name: 'updateTodos',
  args: [state => state.todos],
  builder: ({ action, args: [todos] }) => {
    return (todoListName) => action(todoListName, todos)
  }
})(SaveTodosButton)