-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathDocumentTitle.react.js
51 lines (45 loc) · 1.29 KB
/
DocumentTitle.react.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
41
42
43
44
45
46
47
48
49
50
51
import {connect} from 'react-redux';
import {Component} from 'react';
import PropTypes from 'prop-types';
class DocumentTitle extends Component {
constructor(props) {
super(props);
const {update_title} = props.config;
this.state = {
title: document.title,
update_title,
};
}
UNSAFE_componentWillReceiveProps(props) {
if (!this.state.update_title) {
// Let callbacks or other components have full control over title
return;
}
if (props.isLoading) {
this.setState({title: document.title});
if (this.state.update_title) {
document.title = this.state.update_title;
}
} else {
if (document.title === this.state.update_title) {
document.title = this.state.title;
} else {
this.setState({title: document.title});
}
}
}
shouldComponentUpdate() {
return false;
}
render() {
return null;
}
}
DocumentTitle.propTypes = {
isLoading: PropTypes.bool.isRequired,
config: PropTypes.shape({update_title: PropTypes.string}),
};
export default connect(state => ({
isLoading: state.isLoading,
config: state.config,
}))(DocumentTitle);