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

react-redux-resolve

v1.0.2

Published

Experimental library to universally resolve your component's initial data.

Downloads

11

Readme

React Redux Resolve

Build Status Coverage Status

Experimental library to universally resolve your component's initial data. Per component you can specify a resolver. On the client side, the resolver is executed in componentDidMount() and on the server-side once you call waitForResolves(renderProps, store). This makes server-side rendering easy to implement!

import { resolve } from 'react-redux-resolve';

@resolve(({ dispatch }) => dispatch(fetchSandwich()))
class MyComponent extends Component {
	render() {
		const { sandwich } = this.props;

		return (
			<Sandwich sandwich={sandwich} />
		);
	}
}

Where fetchSandwich() is something like the below (please note the example doesn't handle errors). This requires the redux-thunk middleware:

function fetchSandwich() {
	return (dispatch) => {
		dispatch({
			type: 'FETCH_SANDWICH_STARTED',
			sandwich: sandwich
		});

		return fetch('http://example.com/api/sandwich')
			.then((response) => response.json())
			.then((sandwich) => {				
				dispatch({
					type: 'FETCH_SANDWICH_SUCCEEDED',
					sandwich: sandwich
				});
			});
	};
}

Now, say you're rendering your app on the server, you can easily access the @resolve(). You can do this by accessing .resolves on the components. We've created a helper method called waitForResolves(renderProps, store) to do this:

import { waitForResolves } from 'react-redux-resolve';

// Set up your server-side rendering like you normally would do.

match({ routes, location }, (error, redirectLocation, renderProps) => {
	if (redirectLocation) {
		// TODO: 3xx
	}
	else if (error) {
		// TODO: handle error
	}
	else if (!renderProps) {
		// TODO: 404
	}
	else {
		// Here we call the helper method `waitForResolves`. It calls
		// all your components's resolve methods and returns a promise
		// which is resolved once all
		waitForResolves(renderProps, store)
			.then(() => {
				// TODO: render html
			});
	}
});

API

resolve(resolver)

@resolve(({ dispatch }) => dispatch(..))
class MyComponent extends Component { };

resolver receives an object with the following keys as argument:

  • dispatch: the store's dispatch function
  • getState: the store's getState function
  • history: the history object from your router
  • params: the params object from the route
  • query: the query object from the route

Please note resolve() returns a new component wrapped with the target component, similar to connect(). To use resolve() in ES5, try the following:

MyComponent = resolve(function(obj) {
	return obj.dispatch(..);
})(MyComponent);