@sooni-hooks/use-title
v1.0.1
Published
React Hook to change page title easy way
Downloads
1
Readme
useTitle
React-hook to change HTML title easy way.
Notice
useTitle is custom React-Hook; So it works on only React environment.
Installation
$ npm install @sooni-hooks/use-title
- Add
import useTitle from "@sooni-hooks/use-title"
in your script. - Done!
How to use
useTitle returns setTitle()
. setTitle takes HTML title as argument. If you put a HTML title in the function, the HTML title is automatically changed!
Example
function App() {
const setTitle = useTitle("defaultTitle");
return (
<div className="App">
<h1 onClick={()=>setTitle("changedTitle")}></h1>
</div>
);
}
In the example code, if user clicks h1
, HTML title will be changed "defaultTitle" to "changedTitle".
Development environment setting
First, you need to install NPM Linux :
$ sudo apt install npm
Windows : Go to download link https://nodejs.org/en/download/Install react and react-dom
$ npm i react react-dom
Full code
import { useEffect, useState } from "react";
const useTitle = (init) => {
const [title, setTitle] = useState(init);
const updateTitle = () => {
const HTML_title = document.querySelector("title");
HTML_title.innerText = title;
};
useEffect(updateTitle, [title]);
return setTitle;
};
export default useTitle;