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 🙏

© 2025 – Pkg Stats / Ryan Hefner

redix

v1.0.22

Published

React Dependency Injection for your components and containers

Downloads

118

Readme

Redix

React Dependency Injection for containers, components, and soon (hopefully) actions

Why should I use this?

  • It enforces good design patterns:
    • Container components for the logic and presentational components for the rendering
    • Reusable Components
  • It makes unit testing your container components easier by facilitating test doubles
  • It makes the unit tests of your containers run faster because besides "mocking" the container's dependencies, it doesn't render the container's child components (and children of the children, and so on) even if you mount it.

How to install

npm install redix --save

How to use it

1 . Your container components should extend Redix Container instead of React Component:

import { Container } from 'redix';

class PhotosContainer extends Container {

2 . Your presentational component should specify all the properties it needs by setting its propTypes (well you should do it either you use Redix Container or not, propTypes are a good practice :). Example:

const Photos = (props) => <h1>He have {props.photos.length} photos</h1>
//The container will automatically pass the following props to the presentational component
Photos.propTypes = {
  photos: React.PropTypes.string
}

3 . The constructor of your container should call this.setComponent(Photos), passing the presentational component we want to render. Example:

class PhotosContainer extends Container {
  constructor(props) {
      super(props);
      this.setComponent(Photos, { mapPropFuncsToThis: props });
  }

By using the second parameter mapPropFuncsToThis when setting the component, props that are functions become functions of the container. In other words instead of this.props.fetchPhotos you can do this.fetchPhotos. You need to use the this.fetchPhotos approach if you want to easily mock that function. Why? props are read only, and many functions are injected by third modules that make it very hard to mock, like actions set by the Redux connect mapDispatchToProps. mapPropFuncsToThis is an easy workaround.

class PhotosContainer extends Container {
  constructor(props) {
     super(props);
     this.setComponent(Photos, { mapPropFuncsToThis: props });
     this.bindThis('fetchPhotos') // this.bindThis is a helper from the Redix Container
     // Instead of this.bindThis('fetchPhotos') you can also do:
     // this.fetchPhotos = this.fetchPhotos.bind(this)
     // To bind many methods at once pass an array of strings, example:
     // this.bindThis(['fetchPhotos', 'fetchUser', 'submitForm'])
   }

  componentDidMount() {
    // instead of this.props.fetchPhotos. This is because we set { mapPropFuncsToThis: props }
    this.fetchPhotos();
  }
}

export default connect(
  (state) => ({ photos: state.photos }),
  { fetchPhotos: actions.getPhotos }
)(PhotosContainer)

See the react-redux example

4 . No render method

Rendering the 'view' is not the concern of the container, that is the concern of the presentational component. Therefore the only thing you should write in the render method of the container is i) what component you want to render and ii) which props you want to pass down. And that is already done by the Redix Container class.

5 . Write a test for your container

import React from 'react';
import { createStore } from 'redux';
import { hookProps, DI } from 'redix';
import { expect } from 'chai';
import { mount } from 'enzyme';
import PhotosContainer from '../../../src/containers/PhotosContainer';
import sinon from 'sinon';
import TestUtils from "react-addons-test-utils";

describe('Photos container', () => {
    it(`should fetch photos and pass them down to the child component`, () => {

		const test = {};
		const FakePhotos = hookProps(test);
		const injectedFunctions = {
			fetchPhotos: sinon.spy(() => (Promise.resolve())),
		};
		const photos = [{
		  "albumId": 1,
		  "id": 1,
		  "title": "accusamus beatae ad facilis cum similique qui sunt",
		  "url": "http://placehold.it/600/92c952",
		  "thumbnailUrl": "http://placehold.it/150/30ac17",
		}];
		const store = createStore(()=>{});
		sinon.stub(store, 'getState').returns({ photos });

		//Example using Enzyme
		const container = mount(
		  <PhotosContainer
			functions={injectedFunctions}
			component={FakePhotos}
		  />,
		  { context: { store: store }}
		);

		//Example using TestUtils and the generic Dependency Injector
		TestUtils.renderIntoDocument(
		<DI store={store}>
		  <PhotosContainer
		    functions={injectedFunctions}
		    component={FakePhotos}
		  />
		</DI>
		);

		//assertions
		expect(injectedFunctions.fetchPhotos.called).to.be.true;
		expect(test.props.photos).to.be.deep.equal(photos);
	})
})

See the react-redux example

Examples

Check this folder /examples

Resources