Practice
You can decorate your class components , which is equivalent to passing the component into a function. Decorators are a flexible and readable way of changing component functionality.
@setTitle('Profile')
class Profile extends React.Component {
//....
}
/*
title is the string that will be set as the document title.
WrappedComponent is what our decorator will receive if
it is placed directly above the component class, as in the example above
*/
const setTitle = (title) => (WrappedComponent) => {
return class extends React.Component {
componentDidMount() {
document.title = title
}
render() {
return <WrappedComponent {...this.props} />
}
}
}
Note. Decorators are a feature that did not make it into ES7, but is currently a stage 2 proposal .
Comments