useRouter/withRouter receive undefined on query in first render

后端 未结 4 920
时光说笑
时光说笑 2021-01-05 15:01

I got a problem with my dynamic route. It look like this

[lang]/abc

I am trying to get query value from [lang] but when I using

4条回答
  •  爱一瞬间的悲伤
    2021-01-05 15:41

    In NextJS 9, one way to ensure route parameters are immediately available for page components is to get them from the context arg passed to getServerSideProps() and pass to the component as props.

    For a page like [id].js,

    export function getServerSideProps(context) {
      return {
        props: {params: context.params}
      };
    }
    
    export default ({params}) => {
      const {id} = params;
      return 
    You opened page with {id}
    ; };

提交回复
热议问题