- yarn add react-router-dom
- npm install react-router-dom
import React from "react";
import { AlarmHeader, AlarmWrap, Back, Title } from "./styles";
๐ import { useHistory } from "react-router-dom";
type Props = {
title?: string;
};
const alarm: React.FC<Props> = ({ title }: Props) => {
๐ const history = useHistory();
return (
<AlarmHeader>
<AlarmWrap>
๐ <Back onClick={() => history.goBack()}>
<img
src={process.env.PUBLIC_URL + "/images/header_back.svg"}
alt="๋ค๋ก๊ฐ๊ธฐ"
/>
</Back>
<Title>{title}</Title>
</AlarmWrap>
</AlarmHeader>
);
};
export default alarm;
useHistory๋ผ๋ Hook์ ์ฌ์ฉํ์ฌ ์ ๋ง ๊ฐ๋จํ๊ฒ ์ด์ ํ์ด์ง๋ก ์ด๋ ํ ์ ์๋ค.
<Back onClick={() => history.goBack()}>
๊ทธ๋ฆฌ๊ณ ๋ฒํผ์๋ค๊ฐ onClick ์ด๋ฒคํธ๋ฅผ ๋ฃ์ด์ฃผ๊ธฐ.
next ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ๋ง๋ค์ด์ง ํ๋ก์ ํธ์๋ next ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ฒด ๋ด์ next/router
๋ผ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํด์ค๋๋ค.
import styled from '@emotion/styled'
๐ import { useRouter } from 'next/router'
const SettingButton = () => {
๐ const router = useRouter()
return (
<Container>
<button type="button" className="save">
์ ์ฅ
</button>
๐ <button type="button" className="back" onClick={() => router.back()}>
๋ค๋ก
</button>
</Container>
)
}