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

@mariocadenas/async-dispatcher

v2.4.0

Published

Higher Order Component to easily manage asynchronous redux actions.

Downloads

16

Readme

Async Dispatcher for Redux Actions

CircleCI

Install

npm install @mariocadenas/async-dispatcher

Usage

You must configure the dispatcher, so it can use the store.dispatch function. To do that, pass the store to the configureDispatcher function.

configureDispatcher: function that receives the store object

Example of configuration:

// file where you create your store
// ...
import { configureDispatcher } from '@mariocadenas/async-dispatcher';

// ...
// random example of store
const store = createStore(rootReducer, composeEnhancers(applyMiddleware(thunk)));
configureDispatcher(store);

export default store;

mapAsyncDispatch: <Object>

  • error: Component you want to be shown when an error throws. It gets the error object as parameter.
  • loading: Component that will be displayed while data is being fetched.
  • actions: An array of asynchronous actions that your component needs data from. Component will receive as props every action so you can call them inside the component to force an update.

This object does not replace mapDispatchToProps, if you need to call async functions inside your component you can still do it. This is only for dispatching asynchronous actions that your component needs data from, but is not in the redux store yet.

Example of use:

import React from 'react';
import { connect } from 'react-redux';
import asyncDispatch from '@mariocadenas/async-dispatcher';
import { fetchTodos } from '@/actions/todos';
import ToDos from '@/components/todos';

const mapStateToProps = state => ({ todos: state.todos });
const mapAsyncDispatch = {
  error: e => <div>Error!</div>,
  loading: () => <div>Loading...</div>,
  actions: [fetchTodos]
};
const ToDosConnected = connect(mapStateToProps)(ToDos);

export default asyncDispatch(mapAsyncDispatch)(ToDosConnected);
// @/components/todos

const Todos = props => {
  // Here you can access to loading, error, fetchTodos, todos, any prop you passed.
  // Any asynchronous action will be here too, so you can force an update ignoring the cache
  return <button onClick={props.fetchTodos}>Refetch todos!</button>;
};

export default Todos;

While data is being fetched, you will see loading component instead of your component. Once data is fetched, your component will be able to connect to the store, and get the data itself.

If an error occurs, you will get the error component, it gets the error message, so you can manage it.

You can use this in every component that needs this data, once its fetched, the function call will be cached, so it doesn't need to call every time a component mounts using this.

What is this trying to solve?

There are situations where you have 2 components in different routes, and both depend on the same data. So one of the components should make the call of the async action. The problem comes when you have an scenario like this

You have a route that displays the component Todos.js, this component has a dependency with a todo-list that you are fetching from your database, for example using an async action fetchTodos, that you have defined in your redux actions.

/todos

This component could be something like this

import React from 'react';
import { connect } from 'react-redux';
import { fetchTodos } from '@/actions/todos';
import ToDos from '@/components/todos';

const mapStateToProps = state => ({
  todos: state.todos
});
const mapDispatchToProps = dispatch => ({
  getTodos: () => dispatch(fetchTodos())
});
const ToDosConnected = connect(
  mapStateToProps,
  mapDispatchToProps
)(ToDos);

export default ToDosConnected;

Now we can fetch inside Todos component all the todos.

But now, we also have this route

/todos/:id

This route lets us see the details of the todo.

The problem is that we have a dependency with the full todo list. So if we reload the page, we will not have the data. And we will not be able to display the component correctly.

We can solve this by fetching all data in the top component, but that is not efficient.

TODO

  • [ ] Improve documentation
  • [ ] Make error and loading properties to be optional, so user can manage them in the component.