Last active
March 2, 2016 23:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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