react props

Hvvany·2023년 2월 27일
0

react

목록 보기
2/2

참고 사이트 _ 리액트 공식 홈페이지
https://reactjs.org/docs/components-and-props.html

함수형

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

클래스형

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

부모요소에서 자식 컴포넌트로 데이터를 보낼 때

main.jsx

<SimpleSlider className={style.store} stores={storeList} />

slider.jsx

const stores = this.props.stores;

처음에 this.stores를 찍으니 안먹혀서 공식 홈페이지에 예시를 보니 함수형에서는 함수 파라미터로 props를 받아서 사용하고 클래스형에서는 그냥 바로 this.props.넘겨준 변수명 으로 사용을 하면 되었다.

결론

공식 홈페이지를 잘 보자... 가장 신뢰할 수 있는 정보!!

profile
Just Do It

0개의 댓글