[英]Map function not working while passing props into React component (TypeScript, React and Next.js)
[英]Access to props passed by URL in React + Next.js + Typescript
使用 Typescript 时,我找不到访问通过 URL 传递的道具的方法。 显示最少代码来说明问题。
Router.push('/task/[uuid]', '/task/' + data.uuid);
/task/[uuid].tsx
:export default class App extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
console.log(props); // {uuid: "", url: {…}}
}
componentDidMount() {
console.log(this.props); // {uuid: "", url: {…}}
if (!this.props.uuid) {
throw new Error("Missing UUID");
}
}
static getInitialProps({ req }: NextPageContext) {
console.log(req); // undefined
}
}
最奇怪的是,当推送URL时,它首先抛出错误,在浏览器中显示错误,但有时它会在几秒钟后恢复并成功。
当直接从浏览器调用 URL 时, uuid 在constructor()
和componentDidMount()
都正确可用,但甚至没有调用getInitialProps()
。
我的问题:
应该是query
static getInitialProps({ query }: NextPageContext) {
console.log(query); // undefined
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.