라우팅 시 getServerSideProps이 수반되고
라우팅이 일어나면 _app.getInitialProps이 실행되므로
import App from 'next/app'
import '../styles/globals.css'
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
MyApp.getInitialProps = async (appContext) => {
const appProps = await App.getInitialProps(appContext)
console.log('getInitailProps!')
return { ...appProps }
}
export default MyApp
import { useRouter } from 'next/dist/client/router'
export default function Home() {
const router = useRouter()
function handleClick() {
router.replace(router.asPath)
}
return <button onClick={handleClick}>Replace!</button>
}
export function getServerSideProps() {
console.log('getServerSideProps')
return {
props: {}, // will be passed to the page component as props
}
}
getInitailProps!
getServerSideProps
getInitailProps!
getServerSideProps
getInitailProps!
getServerSideProps