06. Event
๋ค์ด๊ฐ๊ธฐ์
React ์์๋ ์ง์ ์ ์ธ DOM ์ ์ ๊ทผ๋ณด๋ค๋ State ๋ผ๋ ์ํ ๊ฐ์ ์ด์ฉํ์ฌ DOM ์ ๋ณํ๋ฅผ ์ค๋ค๊ณ ํ์ต๋๋ค. ์ด๋ฒ์๋ Event ๋ฅผ ํ์ตํ์ฌ State ๋ฅผ ๋ค์ํ๊ฒ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด๋ ค๊ณ ํฉ๋๋ค.
์ค์ต
ํ๋ก์ ํธ๋ ์ด๊ธฐ ์ธํ ์ํ๋ก ์งํ๋์ด์ง๋๋ค. +@ ๋ก ์ด์ ๋ styled-component ๋ฅผ ์ด์ฉํ๊ธฐ ๋๋ฌธ์ App.css ํ์ผ์ด ํ์์์ต๋๋ค.
์ฐ๋ฆฌ๊ฐ ์ ๋ ฅํ๋ ๊ฐ์ ๋ฐ๋ผ์ ๋์ด, ๋์ด, ์์์ ๊ฐ์ง ๋ฐ์ค๋ฅผ ๋ง๋ค์ด๋ณด๊ณ ์ ํฉ๋๋ค.
Input ์ ๊ฐ์ ์ ๋ ฅํ์ฌ ์ ๋ ฅ๋ ๊ฐ์ ๋ฐํ์ผ๋ก ์์๋ฅผ ๋ง๋ค๊ณ ์ถ์ต๋๋ค. ๋จผ์ Input, Label Component ๋ฅผ ๋ง๋ค์ด๋ณด๊ฒ ์ต๋๋ค.
// App.js
import React from 'react'
import styled from 'styled-components'
const Label = styled.h2`
color: #333;
`
const Input = styled.input`
padding: 10px 20px;
`
class App extends React.Component {
render () {
return (
<div>
<Label>Width</Label>
<Input />
<Label>Height</Label>
<Input />
<Label>Color</Label>
<Input />
</div>
)
}
}
export default App
Box Component ๋ฅผ ์ค๋นํฉ๋๋ค. Box ๋ ๋์ด, ๋์ด, ์์์ Prop ๋ก ๋ฐ์ ์คํ์ผ์ ๊ตฌ์ฑํฉ๋๋ค.
const Box = styled.div`
${({ width, height, color }) =>
width && height && color && css`
width: ${width}px;
height: ${height}px;
background-color: ${color};
`}
`
์ด๋ฒ์๋ Box ์๊ฒ ๋๊ฒจ์ค State ๋ฅผ ์ถ๊ฐํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ถ๊ฐ๋ State ๋ฅผ Box ์๊ฒ ๋๊ฒจ์ค๋๋ค.
// App.js
import React from 'react'
import styled, { css } from 'styled-components'
const Label = styled.h2`
color: #333;
`
const Input = styled.input`
padding: 10px 20px;
`
const Button = styled.button`
display: block;
background: #03A9F4;
border: none;
color: #fff;
font-weight: bold;
border-radius: 4px;
padding: 5px 10px;
margin-top: 20px;
`
const Box = styled.div`
${({ width, height, color }) =>
width && height && color && css`
width: ${width}px;
height: ${height}px;
background-color: ${color};
`}
`
class App extends React.Component {
state = {
width: 0,
height: 0,
color: ''
}
render () {
const { width, height, color } = this.state
return (
<div>
<Label>Width</Label>
<Input />
<Label>Height</Label>
<Input />
<Label>Color</Label>
<Input />
<Box width={width} height={height} color={color} />
</div>
)
}
}
export default App
์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฒ์ Input ์ ๊ฐ์ ์ ๋ ฅํ๊ณ Input ์ ๋ง๋ State ๊ฐ ๋ณ๊ฒฝ์ด๋๋ฉด์ ๋ณ๊ฒฝ๋ ๊ฐ์ด Box ์๊ฒ ์ ๋ฌ๋๊ธฐ๋ฅผ ์ํฉ๋๋ค.
Input ์ onChange Event ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฐ๋๋ ๊ฐ์ ์บ์นํด๋ณด๊ฒ ์ต๋๋ค.
// App.js
import React from 'react'
import styled, { css } from 'styled-components'
const Label = styled.h2`
color: #333;
`
const Input = styled.input`
padding: 10px 20px;
`
const Box = styled.div`
${({ width, height, color }) =>
width && height && color && css`
width: ${width}px;
height: ${height}px;
background-color: ${color};
`}
`
class App extends React.Component {
state = {
width: 0,
height: 0,
color: ''
}
handleStyles = (e) => {
console.log(e.target)
}
render () {
const { state: { width, height, color }, handleStyles } = this
return (
<div>
<Label>Width</Label>
<Input name="width" onChange={handleStyles}/>
<Label>Height</Label>
<Input name="height" onChange={handleStyles} />
<Label>Color</Label>
<Input name="color" onChange={handleStyles} />
<Box width={width} height={height} color={color} />
</div>
)
}
}
export default App

๊ฐ Input ๋ค์ onChange ๋ฅผ ํตํด handleStyles ํจ์๋ฅผ ํธ์ถํฉ๋๋ค. handleStyles ํจ์๋ event ๊ฐ์ฒด๋ฅผ ๋๊ฒจ๋ฐ์ต๋๋ค. event ๊ฐ์ฒด์๋ ํด๋น ์ด๋ฒคํธ์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์ต๋๋ค. Input ๋ค์ name ๊ฐ์ ๊ฐ์ง๊ณ ์๊ณ Input ์ด๊ธฐ ๋๋ฌธ์ value attribute ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. ์ฐ๋ฆฌ๋ ์ด ๊ฐ๋ค์ ์ด์ฉํ์ฌ state ๋ฅผ ๋ณ๊ฒฝํ๋ ค๊ณ ํฉ๋๋ค.
JSX ์์ Event ๋ฅผ ๊ฑธ์ด์ค๋๋ ํจ์๋ฅผ JSX ๋ด๋ถ๊ฐ ์๋ class ์๋์ชฝ์ผ๋ก ๋นผ์ฃผ๋ ๊ฒ์ด ์ข์ต๋๋ค.
handleStyles ํจ์์ ๋์ด์ค๋ event ๊ฐ์ฒด๋ฅผ ํตํด Input ์ name ๊ณผ value ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
// App.js
handleStyles = ({ target: { name, value }}) => {
console.log(name, value)
}

์ด์ ์ด ๊ฐ๋ค์ ์ด์ฉํด์ State ์ ์ ๋ณด๋ฅผ update ํฉ๋๋ค. State ๋ฅผ update ํ ๋๋ ๋ฐ๋์ setState ๋ผ๋ ํจ์๋ฅผ ์ด์ฉํด์ผํฉ๋๋ค.
๊ธฐ์กด์ State ๋ค์ ์ ์งํ๋ฉด์ ์๋ก ๋ค์ด์จ key, value ๊ฐ๋ง update ํฉ๋๋ค.
// App.js
handleStyles = ({ target: { name, value }}) => {
this.setState({ ...this.state, [name]: value })
}
Hook ์ ์ด์ฉํ๊ธฐ
hook ์ ์์์ ์์๋ณธ Class Component ์๋ ์ด์ง ๋ค๋ฆ ๋๋ค. State ์ ์ธ๊ณผ update๋ useState ๋ผ๋ ๊ฒ์ ์ด์ฉํฉ๋๋ค.
// App.js
import React, { useState } from 'react'
import styled, { css } from 'styled-components'
const Label = styled.h2`
color: #333;
`
const Input = styled.input`
padding: 10px 20px;
`
const Box = styled.div`
${({ width, height, color }) =>
width && height && color && css`
width: ${width}px;
height: ${height}px;
background-color: ${color};
`}
`
function App () {
const [styles, setStyles] = useState({
width: 0,
height: 0,
color: ''
})
const handleStyles = ({ target: { name, value }}) => {
setStyles({ ...styles, [name]: value })
}
const { width, height, color } = styles
return (
<div>
<Label>Width</Label>
<Input name="width" onChange={handleStyles}/>
<Label>Height</Label>
<Input name="height" onChange={handleStyles} />
<Label>Color</Label>
<Input name="color" onChange={handleStyles} />
<Box width={width} height={height} color={color} />
</div>
)
}
export default App
Last updated
Was this helpful?