react-document-title
Version:
A declarative, nested, stateful document.title for React
41 lines (33 loc) • 925 B
JavaScript
;
var React = require('react'),
PropTypes = require('prop-types'),
withSideEffect = require('react-side-effect');
function reducePropsToState(propsList) {
var innermostProps = propsList[propsList.length - 1];
if (innermostProps) {
return innermostProps.title;
}
}
function handleStateChangeOnClient(title) {
var nextTitle = title || '';
if (nextTitle !== document.title) {
document.title = nextTitle;
}
}
function DocumentTitle() {}
DocumentTitle.prototype = Object.create(React.Component.prototype);
DocumentTitle.displayName = 'DocumentTitle';
DocumentTitle.propTypes = {
title: PropTypes.string.isRequired
};
DocumentTitle.prototype.render = function() {
if (this.props.children) {
return React.Children.only(this.props.children);
} else {
return null;
}
};
module.exports = withSideEffect(
reducePropsToState,
handleStateChangeOnClient
)(DocumentTitle);