forked from gaearon/react-document-title
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (33 loc) · 925 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
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);