나만 그래요? 나만 못됐나요?😱
그래서 등장했다 !👏
컴포넌트 내부의 state 변경 시에만 리렌더링이 진행되게 하자 !
function Component() {
const value = calculate();
return <div> {value} </div>
}
function calculate() {
return 10
}
const App = () => {
const [listText, setListText] = useState([]);
return (
<>
<Form
listText={listText}
/>
</>
);
};
const Form = ({ listText}) => {
return (
<Content listText={listText} />
);
};
원래 상태를 기억해 놓고 불필요한 렌더를 하지 않겠다
const Content = React.memo(({ listText }) => {
console.log("Content is rendered");
return (
<ul>
{listText.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
});
const Contents = ({ listText }) => {
return useMemo(() => {
console.log("Content is rendered");
return (
<ul>
{listText.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}, [listText]);
};
useMemo(콜백함수, [의존성배열])
useMemo(() => {
return UI 반환
}, [의존성 배열])
아, 안되는구나? 사용 목적이 아예 다름 !
useEffect(() => {
},[])
const Form = ({ listText, currentText, setListText, setCurrentText }) => {
console.log("Form is rendered");
return (
<form
onSubmit={(e) => {
e.preventDefault();
setListText([...listText, currentText]);
}}
>
<input
name="text"
onChange={(e) => {
setCurrentText(e.target.value);
}}
/>
<input type="submit" />
{useMemo(() => {
console.log("Content is rendered");
return (
<ul>
{listText.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}, [listText])}
</form>
);
};
useCallbak(() => 내부함수(),[의존성배열])
const App = () => {
const [masterOn, setMasterOn] = useState(false);
const [kitchenOn, setKitchenOn] = useState(false);
const [bathOn, setBathOn] = useState(false);
const toggleMaster = useCallback(() => setMasterOn(!masterOn), [masterOn]);
const toggleKitchen = useCallback(
() => setKitchenOn(!kitchenOn),
[kitchenOn]
);
const toggleBath = useCallback(() => setBathOn(!bathOn), [bathOn]);
return (
<>
<Light room="침실" on={masterOn} toggle={toggleMaster} />
<Light room="주방" on={kitchenOn} toggle={toggleKitchen} />
<Light room="욕실" on={bathOn} toggle={toggleBath} />
</>
);
};
export default App;
import React, { useState } from "react";
const Light = React.memo(({ room, on, toggle }) => {
console.log({ room, on });
return (
<button onClick={toggle}>
{room} {on ? "ON" : "OFF"}
</button>
);
});
const App = () => {
const [masterOn, setMasterOn] = useState(false);
const [kitchenOn, setKitchenOn] = useState(false);
const [bathOn, setBathOn] = useState(false);
return (
<>
<Light room="침실" on={masterOn} toggle={() => setMasterOn(!masterOn)} />
<Light
room="주방"
on={kitchenOn}
toggle={() => setKitchenOn(!kitchenOn)}
/>
<Light room="욕실" on={bathOn} toggle={() => setBathOn(!bathOn)} />
</>
);
};
export default App;
import React, { useState, useEffect } from "react";
const Light = React.memo(({ room, on, toggle }) => {
console.log({ room, on });
return (
<button onClick={toggle}>
{room} {on ? "ON" : "OFF"}
</button>
);
});
const App = () => {
const [masterOn, setMasterOn] = useState(false);
const [kitchenOn, setKitchenOn] = useState(false);
const [bathOn, setBathOn] = useState(false);
useEffect(() => {
// 현재 상태 값을 이용하여 토글 함수 업데이트
const toggleMaster = () => setMasterOn(!masterOn);
const toggleKitchen = () => setKitchenOn(!kitchenOn);
const toggleBath = () => setBathOn(!bathOn);
// 버튼에 업데이트된 토글 함수 연결
document.querySelector("#masterButton").addEventListener("click", toggleMaster);
document.querySelector("#kitchenButton").addEventListener("click", toggleKitchen);
document.querySelector("#bathButton").addEventListener("click", toggleBath);
// 컴포넌트가 언마운트될 때 이벤트 리스너 정리
return () => {
document.querySelector("#masterButton").removeEventListener("click", toggleMaster);
document.querySelector("#kitchenButton").removeEventListener("click", toggleKitchen);
document.querySelector("#bathButton").removeEventListener("click", toggleBath);
};
}, [masterOn, kitchenOn, bathOn]); // 상태 값이 변경될 때마다 효과 업데이트
return (
<>
<Light room="bedroom" on={masterOn} />
<Light room="kitchen" on={kitchenOn} />
<Light room="Bathroom" on={bathOn} />
</>
);
};
export default App;