React Hooks
functional component์์ state๋ฅผ ๊ฐ์ง ์ ์๊ฒ ๋ง๋ฆ
class component, componentdidMount, render() ํ ํ์๊ฐ ์์ -> ํ๋์ funciton ์ด ๋จ
์ด๊ฒ์ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ด๋ผ๋ ๋ป์ด๊ธฐ๋ ํจ!
recompose + react = ๋ฆฌ์กํธ ํ !
๐Class component๋ฅผ ์ด์ฉํ react (๊ธฐ์กด ๋ฐฉ์)
๋ฒํผ ํด๋ฆญ์ count ๊ฐ ํ๋์ฉ ์ฆ๊ฐํ๋ ๊ธฐ๋ฅ
import React from "react";
import './App.css';
class App extends React.Component{
state = {
count: 0
};
modify = (n) => {
const { count } = this.state;
// this.setState({count:this.state.count+1})
this.setState(current => {
return {
count: n
};
});
};
render() {
const { count } = this.state;
return (
<>
<div>{count}</div>
<button onClick={() => this.modify(count + 1)}>increase</button>
</>
);
}
}
export default App;
1) class component ์์ฑ
2) state ์ง์
3) this ๋ก ์ฐ๊ฒฐ
๐React Hooks
useState
useState(์ด๊ธฐ๊ฐ)
useState๋ array๋ฅผ ๋ฐํ ํจ
array [ ์ธ์๊ฐ , ์ํํ ํ์ ]
์์ 1) ๋ฒํผ ํด๋ฆญ์ count ์ฆ๊ฐ
import React,{useState} from "react";
import './App.css';
const App = () => {
const [count, setCount] = useState(0);
return (
<>
{count}
<button onClick={() => {
setCount(count+1)
}}>Increment</button>
</>
);
}
export default App;
โญ๏ธ useState
const [ count, setCount ] = useState(0)
const [ value, value๋ฅผ ๋ณ๊ฒฝํ๋ ๋ฒ ] = useState(value์ ์ด๊ธฐ๊ฐ)
Eamil input ๋ฐ๊ธฐ
import React,{useState} from "react";
import './App.css';
const App = () => {
const [email, setEmail] = useState("");
const updateEmail = e => {
const {
target: { value }
} = e
setEmail(value);
}
return (
<>
<input placeholder="Email" value={email} onChange={updateEmail}/>
</>
);
}
export default App;
์ด๋ ๊ฒ ReactHook์ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ค์ ๋ก classํํ๋ก ๊ตฌํํ๋ ๊ฒ๋ค์ ํจ์ํ์ผ๋ก ๊ตฌํ ํ ์ ์๊ฒ ํ๋ฉฐ
๊ตฌํ ๋ฐฉ๋ฒ ๋ํ ๊ฐ๋จํ๋ค.
์ด์ ์์ฃผ ์ฌ์ฉ๋๋ ReactHook๋ค์ ๋ฐฐ์๋ณด์
์ฐธ๊ณ
www.youtube.com/watch?v=yS-BU6eYUDE
'๐ React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] React ์ ํน์ง, JSX ์ดํดํ๊ธฐ (0) | 2021.04.11 |
---|---|
ReactHooks ์ ๋ณตํ๊ธฐ 1ํ ( useState, useClick ) (0) | 2021.04.06 |
[React ] React Router๋ฅผ ์ด์ฉํ์ฌ component๊ฐ์ props ๋๊ฒจ์ฃผ๊ธฐ ( (Link & props) (0) | 2021.03.18 |
[React] ํ๋ก์ ํธ GitHub Pages ๋ฐฐํฌํ๊ธฐ + Axios ๋ฅผํตํด json data ๊ฐ์ ธ์ค๊ธฐ (2) | 2021.03.17 |
[React] setState and Lifecycle (0) | 2021.03.16 |
๋๊ธ