๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“— React

[React] ๋ฆฌ์•กํŠธ์˜ Hooks ์˜ˆ์ œ๋กœ ์ •๋ณตํ•˜๊ธฐ

by Tamii 2021. 3. 18.
๋ฐ˜์‘ํ˜•

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

 

๋Œ“๊ธ€