Skip to content
This repository was archived by the owner on May 7, 2022. It is now read-only.

Commit 526a4c0

Browse files
committed
封装设置文档标题组件
1 parent a57f33a commit 526a4c0

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Created by yongyuehuang on 2017/8/27.
3+
*/
4+
'use strict';
5+
/*
6+
* 该插件作用是设置文档的标题,这样你就不再需要使用react-document-title插件了。
7+
*
8+
* 使用方法非常简单
9+
*
10+
* import SetDocumentTitle from 'path/SetDocumentTitle'
11+
render() {
12+
return (
13+
<SetDocumentTitle title="文档标题">
14+
//这里仅能有一个唯一的root元素。
15+
</SetDocumentTitle>
16+
)
17+
}
18+
*
19+
* */
20+
exports.__esModule = true;
21+
22+
var _react = require('react');
23+
24+
var _react2 = _interopRequireDefault(_react);
25+
26+
var _propTypes = require('prop-types');
27+
28+
var _propTypes2 = _interopRequireDefault(_propTypes);
29+
30+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31+
32+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
33+
34+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
35+
36+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
37+
38+
var SetDocumentTitle = function (_React$Component) {
39+
_inherits(SetDocumentTitle, _React$Component);
40+
41+
function SetDocumentTitle() {
42+
_classCallCheck(this, SetDocumentTitle);
43+
44+
return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
45+
}
46+
47+
SetDocumentTitle.prototype.setTitle = function setTitle() {
48+
var title = this.props.title;
49+
50+
document.title = title;
51+
};
52+
53+
SetDocumentTitle.prototype.componentDidMount = function componentDidMount() {
54+
this.setTitle();
55+
};
56+
57+
SetDocumentTitle.prototype.componentDidUpdate = function componentDidUpdate() {
58+
this.setTitle();
59+
};
60+
61+
SetDocumentTitle.prototype.render = function render() {
62+
return _react2.default.Children.only(this.props.children);
63+
};
64+
65+
return SetDocumentTitle;
66+
}(_react2.default.Component);
67+
68+
exports.default = SetDocumentTitle;
69+
70+
SetDocumentTitle.propTypes = {
71+
title: _propTypes2.default.string.isRequired
72+
};

0 commit comments

Comments
 (0)