1) 하위 컴포넌트에 전달하고자 하는 값과 속성을 정의
2) props를 이용하여 정의된 값과 속성을 전달
3) 전달받은 props를 렌더링
function App() {
return (
<Hello name = "react"/> //Hello라는 자식 컴포넌트
)
}
function Hello(props) {
return <div>
안녕하세요, {props.name} // 파라미터를 통해 props 객체 형태로 전달
</div>
}
function App() {
return (
<Wrapper>
<Hello name="react" color="red/>
<Hello color="pink"/>
<Wrapper>
)
}
function Wrapper({children}) {
return (
<div style={style}>
{children}
</div>
)
}
const [state저장변수, state 갱신 함수] = useState(상태초기값)
//이 코드는 밑에와 같음
const state = useState(first);
const isCheck = state[0];
const isCheckCheck = state[1];
const [count, setCount] =useState(0)
useEffect(() => {
'You clikced ${count} times';
return (
<button onClick={() => setCount(count +1)}
)
})
useState보다 다양한 컴포넌트 상황에 따라 다양한 상태를 다른 값으로 업데이트 해주고 싶을 떄 사용하는 hook
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, 초기값);
return (
<>
Count: {state.count}
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
</>
);
}