본 자료는 생활코딩 이고잉 코치님과 Elice 플랫폼의 자료를 사용하여 정리하였습니다.
Props.함수
형태로 받는다면 <태그 함수명={함수}>
형태로 전달import Route from 'react-router-dom'
입력<Route path="경로" element={<Main /> }>
은 컴포넌트이다.const history = useHistory();
history.push({
pathname: "/detail",
search: `?email=${email&password=${password}`,
});
const location = useLocation();
const searchParams = new URLSearchParams(location.search);
const email = searchParams.get("email");
const password = searchParams.get("password");
const initialState = {
message: "hi"
}
const reducer = (state, action) => {
switch(action.type) {
case 'type1':
return { ... } # 초기값을 사용하려면 state. 형태로 사용
case 'type2':
return { ... }
default:
throw new Error()
}
}
const [state, dispatch] = useReducer(reducer, initialState)
return (
<div>
<button onClick={() => {
dispatch({ type: "type1", payload: "전달하려는 값" })
}}
</button>
</div>
)
dispatch({ type: "type1", payload:" 전달하려는 값" }); # payload는 생략가능하다.
const MyContext = createContext();
return (
<MyContext.Provider value={공유할 값}>
<Welcome /> # 컴포넌트
</MyContext.Provider>
)
const {공유받은 값} = useContext(MyContext);
const inputRef = useRef();
<input type="text" data-testid="input" ref={inputRef} />
console.log(inputRef.current.value)