Skip to content

Instantly share code, notes, and snippets.

@amsardesai
Last active March 2, 2016 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amsardesai/6c173d6d946f7805773f to your computer and use it in GitHub Desktop.
Save amsardesai/6c173d6d946f7805773f to your computer and use it in GitHub Desktop.
Higher order component that sets a component's title in a redux store
import { Component } from "react";
import { addTitle, resetTitle } from "./actions";
var addTitle = title => ComposedComponent => class Enhancer extends Component {
contextTypes = {
store: React.PropTypes.object
}
componentDidMount() {
this.context.store.dispatch(addTitle(title));
}
componentWillUnmount() {
this.context.store.dispatch(resetTitle())
}
render() {
return <ComposedComponent {...this.props} title={title} />;
}
};
export default addTitle;
import { Component } from "react";
import addTitle from "./addTitle";
class DynamicView extends Component {
render() {
return <h1>{this.props.title}</h1>;
}
}
export default addTitle("My title")(MyComponent); // Enhanced component
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment