12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import _ from 'lodash';
- import React, { Component } from 'react';
-
- export default class ContainerWrapper {
- static wrap(containerName, OriginalContainer, store) {
- return class extends Component {
- constructor(props) {
- super(props);
- this._assertId(props);
- this._createState(props);
- }
-
- _assertId(props) {
- if (!props.id) {
- throw new Error(`Container ${containerName} does not have an id!`);
- }
- }
-
- _createState(props) {
- this.state = {
- id: props.id,
- allProps: _.merge({}, props, store.getPropsForContainerId(props.id))
- };
- }
-
- componentWillMount() {
- store.setRefForId(this.state.id, this);
- }
-
- componentWillUnmount() {
- store.cleanId(this.state.id);
- }
-
- componentWillReceiveProps(nextProps) {
- this.setState({
- allProps: _.merge({}, nextProps, store.getPropsForContainerId(this.state.id))
- });
- }
-
- render() {
- return (
- <OriginalContainer
- {...this.state.allProps}
- id={this.state.id}
- key={this.state.id}
- />
- );
- }
- };
- }
- }
|