
1. React hook ์ด๋?
ํจ์ํ ์ปดํฌ๋ํธ๋ ์ปดํฌ๋ํธ ๋ด๋ถ์ state๋ฅผ ๋ง๋ค ์ ์๊ณ , ์ปดํฌ๋ํธ์ ์์ฑ, ๋ณ๊ฒฝ, ์๋ฉธ์ ๋ํ ์ด๋ฒคํธ์ธ ๋ผ์ดํ์ฌ์ดํด API๋ฅผ ์ฌ์ฉํ ์ ์์๋ค. ๊ทธ์ ์์ ์ปดํฌ๋ํธ๊ฐ ์ํค๋ ์ผ๋ง ์ฒ๋ฆฌํ๋ ๋จ์ํ ์ปดํฌ๋ํธ์๋ง ์ฌ์ฉ๋๋ค. ํ์ง๋ง 16.8 ๋ฒ์ ๋ถํฐ ํ (hook)์ด๋ผ๋ ๊ฐ๋ ์ด ๋์ ๋๋ฉฐ, ์ด ๋์ ํด๊ฒฐํ ์ ์๊ฒ ๋๋ค. ๋ ๊ฐ์ง ๊ธฐ๋ฅ์ ํจ์ํ ์ปดํฌ๋ํธ์ "hook into”, ๋์ด์ ์ฐ๊ฒฐ ํ๋ค๊ณ ํด์ "hook"์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค. ๋ํ ํด๋์ค ์ปดํฌ๋ํธ์์ ๊ท์ฐฎ๊ฒ ์ฌ์ฉ๋๋ didMount, render, this ๋ฑ ๋์ , ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์คํ์ผ๋ก ๊ฐ๋ ์ฑ ์๊ฒ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๊ฒ ๋๋ค.
Class Component
import { Component } from 'react';
class Example extends Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
</div>
);
}
}
Functional Component using hooks
import { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
- ํจ์ํ ์ปดํฌ๋ํธ์ ์ฝ๋๊ฐ ํจ์ฌ ๊ฐ๊ฒฐํด์ง๊ณ , ๊ฐ๋ ์ฑ์ด ์ข๋ค.
- ํ์ง๋ง Facebook์ด Class Component๋ฅผ ๊พธ์คํ ์ง์ํ๋ค๊ณ ์ฝ์ํ์ผ๋, ๋ ๊ฐ์ง ๋ฐฉ๋ฒ ๋ชจ๋ ์์๋์
ํจ์ํ ์ปดํฌ๋ํธ์ ์ฒซ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ props๊ฐ์ ์ ๋ฌ๋ฐ๋๋ค.
function FuncComp(props){
return(<h1>{props.count}</h1>)
}
function FuncComp({count}){
return(<h1>{count}</h1>)
}
2. useState
- default ๊ฐ ์์ด useState()๋ฅผ ์์ฑํ๋ฉด, state๊ฐ์ undefined๊ฐ ๋๋ค.
import {useState} from 'react'
function FuncComp(props){
var countState = useState(props.count)
// useState์ ๋ฐํ๊ฐ: ๋ฐฐ์ด
// useState(initValue)
var count = countState[0] // ์ฒซ ๋ฒ์งธ ๊ฐ: state ๊ฐ
var setCount = countState[1] // ๋ ๋ฒ์งธ ๊ฐ: state๋ฅผ ๋ฐ๊พธ๋ ํจ์
return(
<h1>{count}</h1>
<button onClick={()=>setCount(count+1)}>
)
}
์ต์์ ๋ ๋ฒจ์์๋ง ํ ์ ์ฌ์ฉํ ์ ์๋ค.
-hook ์ for๋ฌธ, if๋ฌธ, ์ค์ฒฉ ํจ์ ๋ด์์ ํธ์ถ ๋ ์ ์๋ค.
import {useState} from 'react'
function Component(props){
var [count, setCount] = useState(props.count) โ
function inner() {
var [count, setCount] = useState(props.count) โ
}
if(count){
var [count, setCount] = useState(props.count) โ
}
return(
var [count, setCount] = useState(props.count) โ
<button onClick={()=>setCount(count+1)}></button>
)
}
3. useInput
import {useState} from "react"
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 };
};
const App = () => {
const maxLen = (value) => value.length < 10;
const name = useInput("Mr.", maxLen);
return (
<>
<div className="APP">
<h1>Hello</h1>
<input placeholder="Name" {...name} />
</div>
</>
);
};
- {value: "e", onChange: ƒ onChange()}
- {value: "e2", onChange: ƒ onChange()}
- {value: "e22", onChange: ƒ onChange()}
1. onChange: ๊ฐ์ด ๋ฐ๋ ๋ ๋ง๋ค onChangeํจ์๊ฐ ๊ณ์ ์๋ํ๋ฉฐ ์ ํจ์ฑ๊ฒ์ฌ๋ฅผ ๊ฑฐ์ณ DOM์ refreshํ๋ค.
2. onChangeํจ์๋ event๋ฅผ ๊ฐ๊ณ ์๋ค. ๋ค๋ฅธ function์์ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ ์ ์๊ธฐ ๋๋ฌธ์ ํ๋ช ์ ์ธ ์ผ์ด๋ค. ์ด๋ฒคํธ๋ฅผ ๋ถ๋ฆฌ๋ ํ์ผ, ๋ค๋ฅธ entity์ ์ฐ๊ฒฐํด์ ์ฒ๋ฆฌํ ์ ์๋ค.
4. useTabs
import React, { useState } from "react";
const content = [
{
tab: "Section1",
content: "I am content1"
},
{
tab: "Section2",
content: "I am content2"
}
];
const useTabs = (initialTab, allTabs) => {
const [currentIndex, setCurrentIndex] = useState(initialTab);
if (!allTabs || !Array.isArray(allTabs)) {
return;
}
return {
currentItem: allTabs[currentIndex],
changeItem: setCurrentIndex
};
};
const App = () => {
const { currentItem, changeItem } = useTabs(0, content);
return (
<div className="App">
{content.map((section, index) => (
<button onClick={() => changeItem(index)}>{section.tab}</button>
))}
<div>{currentItem.content}</div>
</div>
);
};
export default App;'๐ Front > โ๏ธ React JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [React Hook ์๋ฆฌ์ฆ 3]: 10๊ฐ์ง Hook๋ค (useMemo, useRef ๋ฑ) (0) | 2021.08.03 |
|---|---|
| [React Hook ์๋ฆฌ์ฆ 2]: useEffect() (0) | 2021.07.22 |
| [React Router] ๋ผ์ฐํฐ ์ฌ์ฉํ๊ธฐ(react-router-dom v5) (0) | 2021.07.20 |
| [๋ฆฌ์กํธ ์ผ๋์ฅ]: Component, State, Props (0) | 2021.02.02 |
| [React ์ ๋ฌธ]: ์ฐ๋ ์ด์ , ํน์ง, CRA, CRA๊ตฌ์ถ (0) | 2021.01.26 |