-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Meta } from 'docsComponents'; | ||
|
||
<Meta title="useTitle" /> | ||
|
||
# useTitle | ||
|
||
useTitle sets document title when a title is passed. | ||
|
||
## Signature | ||
|
||
```ts | ||
function useTitle(title?: string): void; | ||
``` | ||
|
||
### Example | ||
|
||
Set document title with useTitle. | ||
|
||
```js live withRender | ||
function TitleComponent() { | ||
const [title, setTitle] = useState(); | ||
useTitle(title); | ||
|
||
return ( | ||
<> | ||
<H3>Type to set document title</H3> | ||
<TextInput value={title} onChange={(ev) => setTitle(ev.target.value)} /> | ||
</> | ||
); | ||
} | ||
|
||
render(<TitleComponent />); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useTitle } from './useTitle'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useTitle } from './useTitle'; | ||
|
||
describe('useTitle', () => { | ||
beforeEach(() => { | ||
document.title = 'initial title'; | ||
}); | ||
|
||
it('sets title', () => { | ||
renderHook(() => useTitle('Hello World')); | ||
expect(document.title).toBe('Hello World'); | ||
}); | ||
|
||
it('does not change title when no title is passed', () => { | ||
renderHook(() => useTitle()); | ||
expect(document.title).toBe('initial title'); | ||
}); | ||
|
||
it('does not change title when empty string is passed', () => { | ||
renderHook(() => useTitle('')); | ||
expect(document.title).toBe('initial title'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* useTitle sets document title when a title is passed. | ||
*/ | ||
export function useTitle(title?: string): void { | ||
useEffect(() => { | ||
if (title) { | ||
document.title = title; | ||
} | ||
}, [title]); | ||
} |