Skip to content

Commit

Permalink
feat(useTitle): reworked hook to make it synchronous without useUpdate;
Browse files Browse the repository at this point in the history
  • Loading branch information
xobotyi committed Nov 4, 2019
1 parent d4ff80a commit a133267
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/useTitle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ describe('useTitle', () => {
expect(useTitle).toBeDefined();
});

const hook = renderHook(props => useTitle(props), { initialProps: 'My page title' });

it('should update document title', () => {
const hook = renderHook(props => useTitle(props), { initialProps: 'My page title' });

expect(document.title).toBe('My page title');
hook.rerender('My other page title');
expect(document.title).toBe('My other page title');
Expand Down
10 changes: 6 additions & 4 deletions src/useTitle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useEffect } from 'react';
import { useRef } from 'react';

const useTitle = (title: string) => {
useEffect(() => {
document.title = title;
}, [title]);
const t = useRef<string>();

if (t.current !== title) {
document.title = t.current = title;
}
};

export default useTitle;

0 comments on commit a133267

Please sign in to comment.