함수 컴포넌트
const Test= () => {
JavaScript영역
return(
컴포넌트함수/JSX영역
)
}
return(이 안은 컴포넌트 함수영역 JSX영역 화면에 보이는 영역)
!!React에서 컴포넌트간 데이터를 전달하는 방식!!
<welcome name="Jo"/>
function parent(props){
return <h1>Hello, {props.test}!</h1?
}
//사용
부모컴포넌트가 자식컴포넌트 사이에 전달한 JSX요소를 의미
destructuring assignment
컴포넌트에 전달되는 props가 객체로 전달되는데 비구조 할당을 사용하면 props객체에서 원하는 값만 꺼내올 수 있다.
//비구조할당 X
function Test1(props){
return(
<h1>
Hello, {props.name}!
</h1>
)
}
//비구조할당 O
function Test2({name}){
return(
<h1>
Hello, {name}!
</h1>
)
}
useState비구조화 할당시 비구조화 할당을 통해 변수와 setter를 분리할 수 있다.
import {useState} from 'react'
function Test(){
//비구조화 할당을 사용하여 state와 setState분리
const[count, setCount] = useState(0) //count의초기값은0
return(
<div>
<p>{count}</p>
<button onClick={()=> setCount(count +1)}>
증가
</button>
</div>
)
//버튼을 누르면 setCount()에 의해 count의 값이 +1되는 컴포넌트
}
//비구조할당2
useEffect(() => {
const {id, name} = user; //user객체에서 id와 name을 비구조화 추출
console.log(`User: ${id} / ${name}`);//``백팁을 사용하여 변수사용
}, [user]);