Props (μμ±)
- λΆλͺ¨ -> μμ μ»΄ν¬λνΈλ‘ λ°μ΄ν°λ₯Ό μ λ¬ν λ μ¬μ©νλ€.
- μ½κΈ° μ μ©μ΄λ©°, μμ μ»΄ν¬λνΈμμ μμ ν μ μλ€.
// λΆλͺ¨ μ»΄ν¬λνΈ
import React from 'react';
import ChildComponent from './ChildComponent';
const ParentComponent = () => {
const data = "μ λ¬ν λ°μ΄ν°";
return <ChildComponent data={data} />;
};
// μμ μ»΄ν¬λνΈ
import React from 'react';
const ChildComponent = (props) => {
return <div>{props.data}</div>;
};
State (μν)
- μ»΄ν¬λνΈ λ΄λΆμμ κ΄λ¦¬ νλ λ°μ΄ν°μ΄λ€.
- μ»΄ν¬λνΈμ μνκ° λ³νν λλ§λ€ Reactλ μλμΌλ‘ λλλ§νλ€.
- Stateμ μ΄κΈ°νλ μ»΄ν¬λνΈμ constructorμμ μννλ€.
- State λ³κ²½ μ, Stateλ₯Ό μ§μ μμ ν΄μλ μλκ³ , SetState λ©μλλ₯Ό μ¬μ©νμ¬ λ³κ²½μ λ°μνλ€.
- ν΄λμ€ν μ»΄ν¬λνΈμμλ this.Stateλ‘ Stateμ μ κ·Όνκ³ , ν¨μν μ»΄ν¬λνΈμμλ useState ν μ μ¬μ©νλ€.
// ν΄λμ€ν μ»΄ν¬λνΈμμμ state
import React, { Component } from 'react';
class ExampleComponent extends Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
}
incrementCount = () => {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.incrementCount}>Increment</button>
</div>
);
}
}
// ν¨μν μ»΄ν¬λνΈμμμ state
import React, { useState } from 'react';
const ExampleComponent = () => {
const [count, setCount] = useState(0);
const incrementCount = () => {
setCount(count + 1);
}
return (
<div>
<p>Count: {count}</p>
<button onClick={incrementCount}>Increment</button>
</div>
);
}
λ°μν
'Frontend > React' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
React | μλͺ μ£ΌκΈ° λ©μλ(Lifecyle Methods) (0) | 2023.11.28 |
---|---|
React | κ°μ²΄ νμ State μ λ°μ΄νΈ (0) | 2023.11.27 |
React | λ°°μ΄ νμ State μ λ°μ΄νΈ (1) | 2023.11.24 |
React | λ³μ vs State (2) | 2023.11.23 |
React | event (μ΄λ²€νΈ) μ λν΄μ (0) | 2023.11.18 |