# useInput
import React, { useState } from "react";
import "./styles.css";
export const useInput = (initialValue, validator) => {
const [value, setValue] = useState(initialValue);
const onChange = (event) => {
const {
target: { value }
} = event;
let willUpdate = true;
if (typeof validator === "function") {
willUpdate = validator(value);
}
if (willUpdate) {
setValue(value);
}
};
return { value, onChange };
};
export default function App() {
const maxLen = (value) => value.length <= 10;
// const maxLen = (value) => !value.includes("@");
const name = useInput("Mr.", maxLen);
return (
<div className="App">
<h1>Hello</h1>
<input placeholder="Name" {...name} />
</div>
);
}
# useTabs
import React, { useState } from "react";
import ReactDOM from "react-dom";
const content = [
{
tab: "Section 1",
content: "I'm the content of the Section 1"
},
{
tab: "Section 2",
content: "I'm the content of the Section 2"
}
];
const useTabs = (initialTab, allTabs) => {
if (!allTabs || !Array.isArray(allTabs)) {
return;
}
const [currentIndex, setCurrentIndex] = useState(initialTab);
return {
currentItem: allTabs[currentIndex],
changeItem: setCurrentIndex
};
};
export default function App() {
const { currentItem, changeItem } = useTabs(0, content);
return (
<div className="App">
{content.map((section, index) => (
<button key={index} onClick={() => changeItem(index)}>
{section.tab}
</button>
))}
<div>{currentItem.content}</div>
</div>
);
}
# useEffect
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
export default function App() {
const sayHello = () => console.log("hello");
const [number, setNumber] = useState(0);
const [aNumber, setAnumber] = useState(0);
useEffect(sayHello, []);
return (
<div className="App">
<div>Hi</div>
<button
onClick={() => {
setNumber(number + 1);
}}
>
{number}
</button>
<button
onClick={() => {
setAnumber(aNumber + 1);
}}
>
{aNumber}
</button>
</div>
);
}
# useTitle
import React, { useEffect, useState } 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;
};
export default function App() {
const titleUpdater = useTitle("Loading....");
setTimeout(() => titleUpdater("Home"), 5000);
return (
<div className="App">
<div>Hi</div>
</div>
);
}
# useClick
import React, { useEffect, useRef, useState } from "react";
import ReactDOM from "react-dom";
const useClick = (onClick) => {
if (typeof onClick !== "function") {
return;
}
const element = useRef();
useEffect(() => {
if (element.current) {
element.current.addEventListener("click", onClick);
}
return () => {
if (element.current) {
element.current.removeEventListener("click", onClick);
}
};
}, []);
return typeof onClick !== "function" ? undefined : element;
};
export default function App() {
const sayHello = () => console.log("say hello");
const title = useClick(sayHello);
return (
<div className="App">
<h1 ref={title}>Hi</h1>
</div>
);
}
✅ componentDidMount
- 컴포넌트가 마운트된 직후에 호출.
✅ componentWillUnMount
- 컴포넌트가 언마운트되기 전에 호출
✅ componentDidUpdate
- 의존성 배열에 포함된 값들이 변경 될 때마다 호출
// componentDidMount 예시
useEffect(() => {
// 컴포넌트가 마운트된 후에 실행되는 코드
// 초기화 작업, 데이터 가져오기 등을 수행
}, []);
// componentWillUnmount 예시
useEffect(() => {
return () => {
// 컴포넌트가 언마운트되기 전에 실행되는 코드
// 리소스 정리, 이벤트 리스너 해제 등을 수행
};
}, []);
// componentDidUpdate 예시
useEffect(() => {
// 의존성 배열에 포함된 값이 변경될 때마다 실행되는 코드
// 이전 값과 현재 값을 비교하여 추가 작업을 수행
}, [dependency]);
# useConfirm
import React, { useEffect, useRef, useState } from "react";
import ReactDOM from "react-dom";
const useConfirm = (message = "", callback) => {
if (typeof callback !== "function") {
return;
}
const confitmAction = () => {
if (confirm(message)) {
callback;
}
};
return confitmAction;
};
export default function App() {
const deleteWorld = () => console.log("Delete the world");
const confirmDelete = useConfirm("Are you sure", deleteWorld);
return (
<div className="App">
<button onClick={confirmDelete}>Delete the world</button>
</div>
);
}