Skip to content

Commit 56dba6f

Browse files
authored
Retaining the modified title on component unmount made optional. (#2)
* Option added to retain the modified title or to rollback to the default title ON the corresponding component unmount event (e.g. route change -> title change for those using this hook, but rollback to the default/original title for those routes not using this hook.) * Using useRef to get the default title.
1 parent 3f1e3db commit 56dba6f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
'use strict';
2-
let { useEffect } = require('react');
2+
let { useRef, useEffect } = require('react');
3+
4+
function useDocumentTitle(title, retainOnUnmount = false) {
5+
const defaultTitle = useRef(document.title);
36

4-
function useDocumentTitle(title) {
57
useEffect(() => {
68
document.title = title;
7-
}, [title])
9+
10+
return () => {
11+
if (!retainOnUnmount) document.title = defaultTitle.current;
12+
};
13+
}, [title]);
814
}
915

1016
module.exports = useDocumentTitle;

0 commit comments

Comments
 (0)