๐ŸคRedux - Part 2. React + Redux (1)

๐Ÿค์ฝ”๋ชฝ์–ด์Šค์˜ ํˆฌ๋‘์•ฑ์„ ๋ฆฌ๋•์Šค๋กœ ๊ฐˆ์•„์—Ž๊ธฐ ์œ„ํ•œ ์ „์ดˆ๊ธฐ์ง€.

๊ทธ๊ฒƒ์€ ๋ฐ”๋กœ ์ด ๊ธ€์—์„œ ๋ถ€ํ„ฐ..!

1. React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ฐ, react-redux, react-router-dom ์„ค์น˜

2. App.js ์—์„œ Route ๋กœ ํด๋ผ์ด์–ธํŠธ ์—”๋“œํฌ์ธํŠธ ๋ถ„๊ธฐ ๋ฐ Home.js, Detail.js ์ƒ์„ฑ

import React from 'react'
import { HashRouter as Router, Route } from 'react-router-dom'
import Home from '../routes/Home'
import Detail from '../routes/Detail'

const App = () => {
  return (
    <Router>
      <Route path="/" exact component={Home}></Route>
      <Route path="/:id" exact component={Detail}></Route>
    </Router>
  )
}

export default App

3. Home.js ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ

๋ฃจํŠธ ๊ฒฝ๋กœ์—์„œ ๋ณด์—ฌ์ง€๊ฒŒ ๋  ํˆฌ๋‘ ์•ฑ์„ ๊ทธ๋Œ€๋กœ ๋งŒ๋“ ๋‹ค?

๊ทธ๋ƒฅ Hooks, DOM ์œผ๋กœ ์ถ”๊ฐ€๊นŒ์ง€ ๋งŒ๋“ ๋‹ค.

import React, { useState } from 'react'

const Home = () => {
  const [text, setText] = useState('')

  const handleChange = event => {
    setText(event.target.value)
  }

  const handleSubmit = event => {
    event.preventDefault()
    // console.log(text);
    const li = document.createElement('li')
    const ul = document.querySelector('ul')

    li.innerText = text
    ul.appendChild(li)

    setText('')
  }
  return (
    <>
      <h1>๐ŸคSomething To Do๐Ÿค</h1>
      <form onSubmit={handleSubmit}>
        <input type="text" value={text} onChange={handleChange} />
        <button>Add</button>
      </form>
      <ul></ul>
    </>
  )
}

export default Home

์‘?

react redux 1

4. ๋ฆฌ๋•์Šค๋กœ ๋ฐ”๊นŒ์น˜๊ธฐ ํ•˜๊ธฐ

์œผ์žŒใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

์ด์ œ ๋Œ€๋žต ๋‚œ๊ฐํ•ด์กŒ๋‹ค.. gg..


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook