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

redux-container-builder

v1.0.17

Published

A simpler fluent interface to redux's connect

Downloads

37

Readme

redux-container-builder

A simpler fluent interface to redux's connect with support for custom HOC's

API:

constructor(component)

Receives the react component to wrap.

mapStateToProps(mappingObject)

A shorthand version of redux's mapStateToProps. Each key in mappingObject describes a prop name, and each correspending value is a selector function that expects the redux state tree as a single parameter.

Calling this method or mapDispatchToProps() will wrap the component in redux's connect method.

mapDispatchToProps(mappingObject)

A shorthand version of redux's mapDispatchToProps. Each key in mappingObject describes a prop name, and each correspending value is a function that will be later invoked and wrapped with dispatch().

Calling this method or mapStateToProps() will wrap the component in redux's connect method.

withWrapper(hoc)

Attach a special one-off higher-order component. HOC's will be applied in the same order as the calls to withWrapper For a more powerful approach, check out Extending the builder.

build()

Builds the component!

Example:

import ContainerBuilder from 'redux-container-builder';

export default new ContainerBuilder(MonthlyExpenseReport)
    .mapStateToProps({
        report: getReport,
        groupedExpenses: getGroupedExpenses,
        isSaving: getIsSaving
    })
    .mapDispatchToProps({
        goBack: () => push('/'),
        saveReport: report => actions.save.request(report),
        tagCategory: ({ id, category }) => actions.tagCategory({ id, category })
    })
    .withWrapper(CustomHOCLibrary())
    .build();

Extending the builder:

It is possible to extend the container builder with your own methods, each one supplying a custom higher-order component you use often in your app.

This can be achived with a static method extend.

ContainerBuilder.extend(name, extendCallback, shouldRunBeforeConnect = false)

  • name: The method name we will attach to the container builder.

  • extendCallback: A function that accepts another callback as its single argument. This second callback must be passed a third function that can be invoked from every ContainerBuilder instance, and returns a custom HOC.

    After finishing nursing a new headache having imagined all these callbacks, please see the example below.

  • shouldRunBeforeConnect: Should the HOCs constructed by the extension methods applied before (or above) the react-redux connect HOC. Pretty much only useful if your HOC uses redux-connect itself and its props must be updated before the original component's. (e.g. redux-fetch-on-mount)

Note: extend() calls can be chained.

Another Note: HOC's are applied to components in the same order that their factory methods are called on the ComponentBuilder instance.

Example - adding translations support to containers using react-i18next:

In order to tap into the translations capabilities of react-i18next, a higher-order-component must be applied to your react components. (Docs)

Here's how to extend ContainerBuilder with a custom "withTranslations" method that does just that:

boostrap.js:

import ContainerBuilder from 'redux-container-builder';
import { translate } from 'react-i18next';

    ContainerBuilder.extend('withTranslations', addToBuilder => {
                        addToBuilder(namespace => translate(namespace));
                    });
                    
...
ImportantComponent.js:

export default new ContainerBuilder(ImportantComponent)
    .withTranslations('ImportantComponent-translation-namespace')
    .mapStateToProps({
        report: getImportantDataSelector
    })
    .component;