react 뒤로 가기 처리 소스 알려줘
- User
react 뒤로 가기 처리 소스 알려줘
Assistant
react에서 뒤로 가기 처리를 하려면, 주로 react-router-dom 라이브러리의 useHistory 훅을 사용해. 이 훅을 사용하면 브라우저의 히스토리 스택을 조작할 수 있고, goBack 메서드를 호출해 이전 페이지로 이동할 수 있어.
예를 들어, 다음과 같은 코드를 작성할 수 있어:
jsximport { useHistory } from 'react-router-dom'; function MyComponent() { const history = useHistory(); const handleGoBack = () => { history.goBack(); }; return ( <button onClick={handleGoBack}>뒤로 가기</button> ); }이 코드는 MyComponent라는 컴포넌트를 정의하고, 이 컴포넌트 내에서 useHistory 훅을 사용해 history 객체를 얻어. 그리고 handleGoBack 함수를 정의해, 이 함수가 호출되면 history.goBack() 메서드를 호출해 이전 페이지로 이동하도록 해.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.