[React] useTtitle

[React] useTtitle

728x90

useTitle

문서 제목을 업데이트하는 hook.

import React, { useState, useEffect } from "react"; import ReactDOM from "react-dom"; const useTitle = (initialTitle) => { const [title, setTitle] = useState(initialTitle); const updateTitle = () => { const htmlTitle = document.querySelector("title"); htmlTitle.innerText = title; }; useEffect(updateTitle, [title]); return setTitle; }; const App = () => { const titleUpdated = useTitle("Loading..."); setTimeout(() => { titleUpdated("Home"); }, 5000); return ; }; export default App;

참고자료: 노마드코더 , '실전형 리액트 Hooks 10개'

반응형

from http://codingbucks.tistory.com/239 by ccl(A) rewrite - 2021-12-27 23:26:59