React.JS) 3. useEffect 사용하기

송민경's avatar
Sep 23, 2024
React.JS) 3. useEffect 사용하기

1. 클릭 이벤트 만들기

  • state의 변경이 감지되면 전체가 렌더링됨
import { useState } from "react"; function App() { const [counter, setValue] = useState(0); const onClick = () => setValue((prev) => prev + 1); console.log("렌더링 됨"); return ( <div> <h1>{counter}</h1> <button onClick={onClick}>click me</button> </div> ); } export default App;
notion image
  • useEffect 사용하기
    • 1번만 렌더링되게 하기
    • notion image
    • index.js에서 <React.StrictMode> 제거하기
      • 2번씩 렌더링되는 문제 해결
      • import React from 'react'; import ReactDOM from 'react-dom/client'; import App from './App'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<App />);
      • App.js
      • import { useState, useEffect } from "react"; function App() { const [counter, setValue] = useState(0); const onClick = () => setValue((prev) => prev + 1); console.log("i run all the time"); const iRunOnlyOnce = () => { console.log("i run only once"); } useEffect(iRunOnlyOnce, []); return ( <div> <h1>{counter}</h1> <button onClick={onClick}>click me</button> </div> ); } export default App;
        notion image
 

2. 검색 바 만들기

import { useState, useEffect } from "react"; function App() { const [counter, setValue] = useState(0); const onClick = () => setValue((prev) => prev + 1); console.log("i run all the time"); useEffect(() => { console.log("i run only once"); }, []); return ( <div> <h1>{counter}</h1> <button onClick={onClick}>click me</button> </div> ); } export default App;
notion image
  • seach bar 만들기
import { useState, useEffect } from "react"; function App() { const [counter, setValue] = useState(0); const [keyword, setKeyword] = useState(""); const onClick = () => setValue((prev) => prev + 1); const onChange = (event) => setKeyword(event.target.value); console.log("i run all the time"); useEffect(() => { console.log("i run only once"); }, []); return ( <div> <input value={keyword} onChange={onChange} type="text" placeholder="Search Here" /> <h1>{counter}</h1> <button onClick={onClick}>click me</button> </div> ); } export default App;
notion image
  • 이벤트 추가하기
    • 입력될때마다 감지되고 클릭이벤트를 실행해도 같이 렌더링됨
    • import { useState, useEffect } from "react"; function App() { const [counter, setValue] = useState(0); const [keyword, setKeyword] = useState(""); const onClick = () => setValue((prev) => prev + 1); const onChange = (event) => setKeyword(event.target.value); console.log("i run all the time"); useEffect(() => { console.log("i run only once"); }, []); console.log("search for", keyword); return ( <div> <input value={keyword} onChange={onChange} type="text" placeholder="Search Here" /> <h1>{counter}</h1> <button onClick={onClick}>click me</button> </div> ); } export default App;
      notion image
  • useEffect 사용하기
import { useState, useEffect } from "react"; function App() { const [counter, setValue] = useState(0); const [keyword, setKeyword] = useState(""); const onClick = () => setValue((prev) => prev + 1); const onChange = (event) => setKeyword(event.target.value); console.log("i run all the time"); useEffect(() => { console.log("i run only once"); }, []); useEffect(() => { console.log("search for", keyword); }, [keyword]); return ( <div> <input value={keyword} onChange={onChange} type="text" placeholder="Search Here" /> <h1>{counter}</h1> <button onClick={onClick}>click me</button> </div> ); } export default App;
notion image
  • 시작될 때 검색안되게 하기
    • import { useState, useEffect } from "react"; function App() { const [counter, setValue] = useState(0); const [keyword, setKeyword] = useState(""); const onClick = () => setValue((prev) => prev + 1); const onChange = (event) => setKeyword(event.target.value); console.log("i run all the time"); useEffect(() => { console.log("i run only once"); }, []); useEffect(() => { if(keyword !== ""){ console.log("search for", keyword); } }, [keyword]); return ( <div> <input value={keyword} onChange={onChange} type="text" placeholder="Search Here" /> <h1>{counter}</h1> <button onClick={onClick}>click me</button> </div> ); } export default App;
      notion image

3. count에도 추가해보기

import { useState, useEffect } from "react"; function App() { const [counter, setValue] = useState(0); const [keyword, setKeyword] = useState(""); const onClick = () => setValue((prev) => prev + 1); const onChange = (event) => setKeyword(event.target.value); useEffect(() => { console.log("i run only once"); }, []); useEffect(() => { if(keyword !== ""){ console.log("i run when 'keyword' changes" , keyword); } }, [keyword]); useEffect(() => { console.log("i run when 'counter' changes" , counter); }, [counter]); return ( <div> <input value={keyword} onChange={onChange} type="text" placeholder="Search Here" /> <h1>{counter}</h1> <button onClick={onClick}>click me</button> </div> ); } export default App;
notion image
Share article

vosw1