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-provide

v0.1.2

Published

Redux Data Provider for ReactJS components

Downloads

1

Readme

TravisCI

redux-provide

Easily create Higher Order Components that allow you to connect any component to whatever part of the Redux State.

Wait, what?

When using Redux, you usually connect a Smart Component to some part of your state in order to be able to either read some values from it or dispatch some actions. When your application grows bigger, you'll find that:

  • Many components will probably use the same selectors or connect to the same parts of your Redux State;
  • You'll have to import a lot of Action Creators, usually the same ones, all over the place because you'll need to dispatch them on more than a single smart component;

If you, like me, like to organize your application in modules (see my own application seed) you'll also find that

  • Modules will end up depending on each other, thus defeating the concept of isolation and modularity

This library aims to mitigate these problems by allowing you to create ad-hoc HoCs to connect any component on the fly to whatever part of the Redux State, and provide it with bound action creators as well.

How?

First, install it using npm install redux-provide

Then, simply create a DataProvider file somewhere in your application structure where you define the selectors and the action creators you want to provide, and export it by wrapping it in a provide function call (which is what this library exports):

DataProvider.js
import { createSelector, createStructuredSelector } from 'reselect'
import provide from 'redux-provide'

import { fetchPosts } from './ActionCreators'

const postsSelector = state => state.posts.items
const select = createStructuredSelector({
	posts: postsSelector
})

export default provide(select, { fetchPosts })

You can now easily connect any component by simply wrapping it in the HoC exported by DataProvider.js, like this:

MyComponent.js
import React from 'react'
import WithPosts from './DataProvider'

class MyComponent extends React.Component {
	componentDidMount() {
		// This action creator is bound, so it's already wrapped in a dispatch call
		this.props.fetchPosts()
	}

	render() {
		return (
			<ul>
				{this.props.posts.map( (post, index) => (
					<li key={index}>{post.title}</li>
				) )}
			</ul>
		)
	}
}

export default WithPosts(MyComponent)

Do I really need this?

Well, it's up to you. It provides a layer of abstraction that is almost purely semantic, and it might not play well with your application so your mileage may vary. Also, the whole thing is 8 lines of code, so you probably should just copypaste it into your project. Here it is, for your convenience:

Provide.js
import React from 'react'
import { connect } from 'react-redux'

function provide(select, ActionCreators) {
	return (WrappedComponent) => connect(select, ActionCreators)(WrappedComponent)
}

export default provide

License

MIT