titlehook for dynamic tab titles
This commit is contained in:
@@ -1,10 +1,14 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import {GHButton} from "../components/Button";
|
import {GHButton} from "../components/Button";
|
||||||
|
import {Link} from "react-router-dom";
|
||||||
|
import useTitle from "../../utils/TitleHook";
|
||||||
|
|
||||||
const ROOM_URL = 'http://localhost:5062/room';
|
const ROOM_URL = 'http://localhost:5062/room';
|
||||||
|
|
||||||
const Home = () => {
|
const Home = () => {
|
||||||
|
|
||||||
|
useTitle('Home');
|
||||||
|
|
||||||
const [rooms, setRooms] = React.useState<string[]>([]);
|
const [rooms, setRooms] = React.useState<string[]>([]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
@@ -21,17 +25,13 @@ const Home = () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const gotoRoom = (roomId: string) => {
|
|
||||||
window.location.href = `/room/${roomId}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<GHButton onClick={handleCreateRoom}>Create Room</GHButton>
|
<GHButton onClick={handleCreateRoom}>Create Room</GHButton>
|
||||||
<ul>
|
<ul>
|
||||||
{
|
{
|
||||||
rooms.map((room, index) => {
|
rooms.map((room, index) => {
|
||||||
return <li key={index} onClick={() => gotoRoom(room)}>{room}</li>
|
return <li key={index}><Link to={`room/${room}`}>{room}</Link></li>
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ import useWebSocket from "react-use-websocket";
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import {useParams} from "react-router";
|
import {useParams} from "react-router";
|
||||||
import {GHButton} from "../components/Button";
|
import {GHButton} from "../components/Button";
|
||||||
|
import useTitle from "../../utils/TitleHook";
|
||||||
|
|
||||||
const Room = () => {
|
const Room = () => {
|
||||||
|
|
||||||
|
useTitle('Mau!');
|
||||||
|
|
||||||
const {roomId} = useParams();
|
const {roomId} = useParams();
|
||||||
|
|
||||||
const WS_URL = `ws://localhost:5062/room/${roomId}`;
|
const WS_URL = `ws://localhost:5062/room/${roomId}`;
|
||||||
@@ -38,7 +41,10 @@ const Room = () => {
|
|||||||
<div>
|
<div>
|
||||||
<h1>Room {roomId}</h1>
|
<h1>Room {roomId}</h1>
|
||||||
<GHButton onClick={handleLeaveRoom}>Leave Room</GHButton>
|
<GHButton onClick={handleLeaveRoom}>Leave Room</GHButton>
|
||||||
<form>
|
<form onSubmit={(event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
handleSend();
|
||||||
|
}}>
|
||||||
<input type="text" name="message" placeholder="Send a message" value={message}
|
<input type="text" name="message" placeholder="Send a message" value={message}
|
||||||
onChange={handleMessageChange}/>
|
onChange={handleMessageChange}/>
|
||||||
<span onClick={handleSend}>Send</span>
|
<span onClick={handleSend}>Send</span>
|
||||||
|
|||||||
14
src/utils/TitleHook.ts
Normal file
14
src/utils/TitleHook.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
const useTitle = (title: string) => {
|
||||||
|
React.useEffect(() => {
|
||||||
|
const prevTitle = document.title;
|
||||||
|
document.title = title;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.title = prevTitle;
|
||||||
|
};
|
||||||
|
}, [title]);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useTitle;
|
||||||
Reference in New Issue
Block a user