...

/

1 Container component

1 Container component

Containers are React components that talk to the Redux data store.

You can think of presentation components as templates that render stuff and containers as smart-ish views that talk to controllers. Or maybe they’re the controllers.

Sometimes it’s hard to tell. Basically, presentation components render and don’t think, containers communicate and don’t render. Redux reducers and actions think.

I’m not sure this separation is necessary in small projects. Maintaining it can be awkward and sometimes cumbersome in mid-size projects, but I’m sure it makes total sense at Facebook scale. We’re using it in this project because it’s the officially suggested way.

The gist of our AppContainer looks like this:

Press + to interact
// src/containers/AppContainer.jsx
import { connect } from 'react-redux';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import App from '../components';
import { tickTime, tickerStarted, startParticles, stopParticles, updateMousePos, createParticles } from '../actions';
class AppContainer extends Component {
componentDidMount() {
const { store } = this.context;
this.unsubscribe = store.subscribe(() =>
this.forceUpdate()
);
}
componentWillUnmount() {
this.unsubscribe();
}
// ...
render() {
const { store } = this.context;
const state = store.getState();
return (
<App {...state}
startTicker={() => this.startTicker()}
startParticles={() => this.startParticles()}
stopParticles={() => this.stopParticles()}
updateMousePos={() => this.updateMousePos()}
/>
);
}
};
AppContainer.contextTypes = {
store: PropTypes.object
};
export default AppContainer;

We import dependencies, then we define AppContainer as a full-feature ...

Access this course and 1400+ top-rated courses and projects.