props

qwe8851·2022년 9월 18일
0

💎 React

목록 보기
7/37

자식컴포넌트에서 부모컴포넌트에 있던 state를 쓰고싶으면 props로 전송해서 써야 함

state 전송 법

  1. <자식컴포넌트 작명 = { state이름 }>
  2. 자식 컴포넌트에서 props 파라미터 등록 후 props.작명 사용
// 부모 컴포넌트
...
{ modal == true ? <Modal 작명={글제목}/> : null }

// 자식 컴포넌트
function Modal(props){
  return(
  	<div className = "moal">
    	<h4>{props.작명}</h4>
		...
    </div>
  )
}

Sample code


Modal component안에 외부의 state 집어넣기

function App (){
  let [state, setState] = useState(['state1', 'state2', 'state3']);
  return (
    <div>
      <Modal state = {state}></Modal>
    </div>
  )
}

function Modal(){
  return (
    <div className="modal">
      <h4>{ state[0] }</h4>
    </div>
  )
}
profile
FrontEnd Developer with React, TypeScript

0개의 댓글