Skip to content

Commit

Permalink
Cleanup function 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
beecomci committed Dec 16, 2021
1 parent 569d67f commit c9b95c0
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
import { useState, useEffect } from 'react';
import Button from './Button';
import styles from './App.module.css';

function App() {
const [counter, setValue] = useState(0);
const [keyword, setKeyword] = useState("");
function Hello() {
const byeFn = () => {
console.log("destroyed"); // 컴포넌트가 destroy 될 때 실행됨
};

const hiFn = () => {
console.log("created"); // Hello 컴포넌트가 create 될 때 맨 처음 1회만 실행됨

const onClick = () => setValue(prev => prev + 1);
const onChange = (event) => setKeyword(event.target.value);
return byeFn;
};

console.log('run all the time');
useEffect(hiFn, []);

useEffect(() => {
console.log('call the api');
}, []);
return <h1>Hello</h1>;
}

function App() {
const [showing, setShowing] = useState(false);

useEffect(() => {
if (keyword && keyword.length > 5) {
console.log('search for', keyword);
}
}, [keyword]);
const onClick = () => setShowing(prev => !prev);

return (
<div>
<input
value={keyword}
onChange={onChange}
type="text"
placeholder="Search here..."
/>
<h1
className={styles.title}>
{counter}
</h1>
<Button
text={"click me"}
onClick={onClick}
/>
{showing ? <Hello /> : null}
<button
onClick={onClick}>
{showing ? "Hide" : "Show"}
</button>
</div>
);
}
Expand Down

0 comments on commit c9b95c0

Please sign in to comment.