Props (property)
props๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ ์์ ์ปดํฌ๋ํธ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ์ ๋ณด์ ์งํฉ์ผ๋ก, ๊ฐ์ฒด ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ํฌํจํ๊ณ ์๋ค.
ํน์ง
- React ๋ด์์ ์์ฑ์ผ๋ก ์ฌ์ฉ๋จ
- ์ฝ๊ธฐ์ ์ฉ(Read-only)์
- ๋ถ๋ชจ๋ก๋ถํฐ ์ ๋ฌ๋ Props๋ ์์ ์ปดํฌ๋ํธ์์ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ
(์ ์๋ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ ค๋ฉด ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํด์ผ ํจ)
์ฌ์ฉ ๋ฐฉ๋ฒ
๋ถ๋ชจ ์ปดํฌ๋ํธ
<์์ ์ปดํฌ๋ํธ props์ด๋ฆ = {props} />
์์ ์ปดํฌ๋ํธ
// ํจ์ํ
function ์์ ์ปดํฌ๋ํธ ({props์ด๋ฆ}){}
// ํด๋์คํ
super(props)
props.props์ด๋ฆ
// ๋๋
this.props.props์ด๋ฆ
์ด์ ๊ธ(์ํ๊ด๋ฆฌ)์์ ์ฌ์ฉํ๋ counter ์ฝ๋๋ฅผ ์ด์ฉํด์ props๋ฅผ ํ์ธํด๋ณด๊ฒ ๋ค.
๋ถ๋ชจ ์ปดํฌ๋ํธ์์ Count(์์ ์ปดํฌ๋ํธ์ ์ด๋ฆ) ์์๋ฅผ ๋ง๋ค๊ณ , ๊ทธ ์์ counter์ hello๋ผ๋ props๋ฅผ ์ ๋ฌํ๊ณ ,
์์ ์ปดํฌ๋ํธ์ธ Count์์ ํ๋ผ๋ฏธํฐ props๋ฅผ ์ถ๊ฐํด ๋ฐ์์ ์ฌ์ฉํ ์ ์๋ค.
์ค์ ๋ก ์ค๊ฐ์ hello prop๋ฅผ ์ถ๊ฐํ ๋ค๋ก๋ hello๋ ์ฝ์์ฐฝ์ ์ถ๋ ฅ๋๊ณ ์์์ ์ ์ ์๋ค.
<Count counter={counter} />
์ด ๋ ์ผ์ชฝ์ counter๋ Count ์ปดํฌ๋ํธ์์ ๋ฐ์ props์ ์ด๋ฆ,
์ค๋ฅธ์ชฝ {counter}๋ App ์ปดํฌ๋ํธ์์ ๊ด๋ฆฌํ๋ counter์ ์ํ ๊ฐ์ด๋ค.
App ์ปดํฌ๋ํธ์์ counter์ ์ํ๋ฅผ Count์ ์ ๋ฌํด ๊ฐ์ ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ๊ฒ์ด๋ค.
์์ฒ๋ผ props ๊ฐ์ฒด๋ฅผ ๋ชจ๋ ๊ฐ์ ธ์์ ๋ counter ๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ๋ฐ์ํ๊ณ ์ถ๋ค๋ฉด ์๋์ ๊ฐ์ด ์จ์ค ์ ์๋ค.
function Count(props) {
console.log(props);
return <div>counter : {props.counter}</div>
}
๋๋ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌ์กฐ๋ถํดํ ๋น์ผ๋ก ๊ฐ์ ธ์ฌ ์๋ ์๋ค.
๊ตฌ์กฐ๋ถํดํ ๋น์ ํ ๋๋ {}๋ก ์ ๋ฐ ๊ผญ ๋ฌถ์ด์ค์ผํ๋ค. ์ํ๋ฉด...
function App() {
const [counter, setCounter] = useState(0)
function increase() {
setCounter((prev)=>prev+1);
}
function decrease() {
setCounter((prev)=>prev-1);
}
return (
<>
<Count counter={counter} hello={"hello"} array={[1, 2, 3, "์๋
ํ์ธ์"]}/>
<button onClick={increase}>+</button>
<button onClick={decrease}>-</button>
</>
);
}
// {}๋ก ๋ฌถ์ด์ ๊ตฌ์กฐ๋ถํดํ ๋น์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
function Count({counter, array, hello}) {
console.log('array', array);
console.log('counter', counter);
console.log('hello', hello);
return <div>counter : {counter}</div>
}
+ ํด๋์ค ์ปดํฌ๋ํธ๋ก ๊ตฌ์ฑํ๊ธฐ
import { Component } from "react";
import "./App.css";
class App extends Component {
state = { counter: 0 };
increase = () => {
this.setState((prevState) => ({
counter: prevState.counter + 1,
}));
};
decrease = () => {
this.setState((prevState) => ({
counter: prevState.counter - 1,
}));
};
render() {
return (
<div>
<Count counter={ this.state.counter }/>
<button onClick={this.increase}>+</button>
<button onClick={this.decrease}>-</button>
</div>
);
}
}
class Count extends Component {
// ๋ฐฉ๋ฒ #1. constructor๋ฅผ ์์ฑํด super ํค์๋๋ฅผ ์ฌ์ฉ
// ๋ฐฉ๋ฒ #2. constructor ์์ฑ ์ํ๊ณ ๊ทธ๋ฅ ๋ฐ์์ค๊ธฐ (๋ง์ฝ ์์ฑ ์ํ ์ ๊ธฐ๋ณธ์ผ๋ก constructor๊ฐ ์์ฑ๋๊ธฐ ๋๋ฌธ์)
constructor(props){
super(props);
}
render(){
return <div>counter : {this.props.counter}</div>
}
}
export default App;
State ๋์ด์ฌ๋ฆฌ๊ธฐ
๋ฆฌ์กํธ์์๋ ๋ฐ์ดํฐ์ ํ๋ฆ์ด ๋ ๋ถ๋ชจ → ์์์ผ๋ก ํฅํ๋ ๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ์ ๊ฐ์ง๋๋ฐ, ์ด ๋ ๋๊ฐ ์ด์์ ์์ ์ปดํฌ๋ํธ ๊ฐ ๋์ผํ ์ํ๋ฅผ ๊ณต์ ํด์ผ ํ ๋ ๊ทธ ์ํ๋ฅผ ๊ณตํต๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ๋ก ์ฌ๋ ค์ผ ํ๋ค. ์ด๋ ๊ฒ ํจ์ผ๋ก์จ ์์ ์ปดํฌ๋ํธ ์ํ๋ฅผ ๊ณต์ ํ๋ฉด์๋ ์๋ก ๋๊ธฐํ๋ UI๋ฅผ ๋ง๋ค ์ ์์ด ๋ฐ์ดํฐ ์ผ๊ด์ฑ ์ ์ง, ๋ณ๊ฒฝ ๊ฐ์ง, ์ฌ์ฌ์ฉ์ฑ์ด ๋ณด์ฅ๋๋ค.
#๋ฐฉ๋ฒ 1 : ์์ ์ปดํฌ๋ํธ์์ ๊ณ์ฐํ๊ธฐ
์ฐ์ ์์ counter ์์ ์์ ๊ธฐ์กด์ ๋ฒํผ๋ค์ PlusBtn, MinusBtn์ ์ปดํฌ๋ํธ๋ก ๋ฐ๋ก ์์ฑํ๋ค.
๊ทธ๋ฆฌ๊ณ input ํ๋๊ฐ ์ถ๊ฐ๋๋ค. ์์์ปดํฌ๋ํธ CounterInput๋ก ์์ฑํ๊ณ , input์ ์ํ๋ ๋ฐ๋ก ์ ์ธํ๋ค. ๊ธฐ๋ฅ์ ๋ค์๊ณผ ๊ฐ๋ค.
* input์ ์ซ์๋ฅผ ์
๋ ฅ => ์
๋ ฅ ๋ฒํผ์ ๋๋ฅด๋ฉด counter์ ๊ฐ์ด ๋ณ๊ฒฝ => ํ๋ฉด์ ๋ฐ์
์ด ๋ ์์ ์ปดํฌ๋ํธ๊ฐ ๊ฐ๊ฐ const [counter, setCounter] = useState(0); ~~~ ๋ฑ ์ํ๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ฉด, ๋ฒํผ๋ผ๋ฆฌ ์ํ๊ฐ ๊ณต์ ๋์ง ์์ counter์ ๊ฐ์ด ์๋ง์ด ๋ ๊ฒ์ด๋ค. ํ์ง๋ง State๋ฅผ ๋์ด์ฌ๋ฆฌ๋ฉด ์์ ์ปดํฌ๋ํธ์์ ์์ ๋กญ๊ฒ ์ํ๋ฅผ ๋ณ๊ฒฝํด ๋ถ๋ชจ์๊ฒ ์ ๋ฌํ ์ ์๋ค.
function App() {
const [counter, setCounter] = useState(0);
const [inputValue, setInputValue] = useState(5);
return (
<>
<Count counter={counter} />
<PlusBtn setCounter={setCounter} />
<MinusBtn setCounter={setCounter} />
<CounterInput inputValue={inputValue} setInputValue={setInputValue} setCounter={setCounter} />
</>
);
}
PlusBtn, MinusBtn ์์ ์ปดํฌ๋ํธ๋ค์๊ฒ props์ setCounter๋ฅผ ๋ด์ ๋๊ฒจ์คฌ๊ณ ,
CounterInput์๊ฒ๋ input์ ์ํ๊ฐ๊ณผ ์ํ ๋ณ๊ฒฝ ํจ์, ๊ทธ๋ฆฌ๊ณ counter์ ๊ฐ๋ ๋ณ๊ฒฝํด์ฃผ๊ธฐ ์ํด setCounter๊น์ง props์ผ๋ก ๋๊ฒจ์คฌ๋ค.
์์ ์ปดํฌ๋ํธ์์๋ ๋ค์๊ณผ ๊ฐ์ด ๋ฒํผ ์์์ ์ํ๋ฅผ ์ง์ ๋ณ๊ฒฝํ๊ณ ์๋ค.
function PlusBtn({ setCounter }) {
return (
<button onClick={() => { setCounter(prev => prev + 1); }}>+</button>
);
}
function MinusBtn({ setCounter }) {
return (
<button onClick={() => { setCounter(prev => prev - 1); }}>-</button>
);
}
function Count({ counter }) {
return <div>counter : {counter}</div>;
}
CounterInput ์ปดํฌ๋ํธ๋ ๋ค์๊ณผ ๊ฐ๋ค.
function CounterInput({ inputValue, setInputValue, setCounter }) {
return (
<div>
<input type="number" value={inputValue} onChange={e => setInputValue(e.target.value)} />
<button onClick={() => setCounter(inputValue)}>์
๋ ฅ</button>
</div>
);
}
input์ value๊ฐ์ {inputValue}๋ก ์ค์ ํ๊ณ , onChange ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ setInputValue์ ๊ฐ์ ์ด๋ฒคํธ ํ๊ฒ์ value(์ฆ input์ ์ ์ ๊ฐ)
๋ง์ฝ onChange์ด๋ฒคํธ๋ฅผ ์ค์ ํ์ง ์์ผ๋ฉด ์ฒ์ ์ ์ธํ ๋์ useState(5)์์ ๋ณ๊ฒฝ๋์ง ์๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ ๋ ฅ ๋ฒํผ์ onClick ์ด๋ฒคํธ๋ฅผ ์ถ๊ฐํด ํด๋ฆญ ์ setCounter๋ก counter์ ๊ฐ์ด inputValue์ ๊ฐ์ผ๋ก ๋ณ๊ฒฝ๋๊ฒํ๋ค.
์ด ๋ ์ด๋ ๊ฒ input ์์์ ๊ฐ๊ณผ ๋ฆฌ์กํธ์ ์ํ์ ์์ ํ ์ฐ๋๋์ด์์ ๋, ํด๋น ์ปดํฌ๋ํธ๋ฅผ Controlled Component ๋ผ๊ณ ๋ ํ๋ค.
#๋ฐฉ๋ฒ 2. ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ๊ณ์ฐ ํจ์๋ฅผ ๋ด๋ ค์ฃผ๊ธฐ
onClick์ ์ํ๋ฅผ ์ง์ ๋ณ๊ฒฝํด์ฃผ๋ ๋ถ๋ถ์ ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ํจ์๋ฅผ ํํํ๊ณ , ํจ์๋ฅผ props๋ก ๋ณด๋ด๊ณ ์๋ค.
๊ทธ๋ฆฌ๊ณ ํด๋ฆญ ์ counter์ ๊ฐ์ inputValue๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์ setCounternumber๋ฅผ ์ถ๊ฐํ๊ธฐ ๋๋ฌธ์,
CounterInput ์ปดํฌ๋ํธ์๋ ๊ธฐ์กด์ setCounter๊ฐ ์๋ setCounternumber๋ฅผ props๋ก ๋๊ฒจ์ฃผ๊ณ ์๋ค.
function App() {
const [counter, setCounter] = useState(0);
const [inputValue, setInputValue] = useState(5);
const incrementCounter = () => {
setCounter(counter + 1);
};
const decrementCounter = () => {
setCounter(counter - 1);
};
const setCounternumber = () => {
setCounter(inputValue);
};
return (
<>
<Count counter={counter} />
<PlusBtn incrementCounter={incrementCounter} />
<MinusBtn decrementCounter={decrementCounter} />
<CounterInput inputValue={inputValue} setInputValue={setInputValue} setCounternumber={setCounternumber} />
</>
);
}
์์ ์ปดํฌ๋ํธ๋ค์ ๋ค์๊ณผ ๊ฐ๋ค.
setCounterํด์ฃผ๋ ์คํ๋๋ ๋ด์ฉ์ ๋ถ๋ชจ์์ ์ ์ธํด props๋ก ๋ค ๋ณด๋๊ธฐ ๋๋ฌธ์ onClick์ ์คํ ๋ด์ฉ ํจ์๋ฅผ ๊ทธ๋๋ก ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
function CounterInput({ inputValue, setInputValue, setCounternumber }) {
return (
<div>
<input type="number" value={inputValue} onChange={e => setInputValue(e.target.value)} />
<button onClick={setCounternumber}>์
๋ ฅ</button>
</div>
);
}
function PlusBtn({ incrementCounter }) {
return <button onClick={incrementCounter}>+</button>;
}
function MinusBtn({ decrementCounter }) {
return <button onClick={decrementCounter}>-</button>;
}
function Count({ counter }) {
console.log('counter', counter);
return <div>counter : {counter}</div>;
}
+ ํด๋์ค ์ปดํฌ๋ํธ๋ก ๊ตฌ์ฑํ๊ธฐ
class App extends Component {
state = { counter: 0 };
incrementCounter = () => {
this.setState({counter : this.state.counter + 1})
};
decrementCounter = () => {
this.setState({counter : this.state.counter - 1})
}
render() {
return (
<>
<Count counter={ this.state.counter }/>
<PlusBtn incrementCounter={this.incrementCounter}/>
<MinusBtn decrementCounter={this.decrementCounter} />
</>
);
}
}
class PlusBtn extends Component {
render(){
return (
<button onClick={this.props.incrementCounter}>+</button>
)
}
}
class MinusBtn extends Component {
render(){
return (
<button onClick={this.props.decrementCounter}>-</button>
)
}
}
class Count extends Component {
render(){
return <div>counter : {this.props.counter}</div>
}
}
'react' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React / ์กฐ๊ฑด๋ถ ๋ ๋๋ง (0) | 2025.04.09 |
---|---|
React / state, props๋ฅผ ์ด์ฉํด CRUD ๊ธฐ๋ฅ์ ๊ฐ์ง Todo-List ๋ง๋ค๊ธฐ (0) | 2025.04.07 |
React / SPA vs MPA (0) | 2025.04.04 |
React / ์ํ๊ด๋ฆฌ (0) | 2025.04.04 |
React / map, filter๋ก ๋ฐ๋ณต ๋ ๋๋งํ๊ธฐ (0) | 2025.04.03 |