kiv*_*vul 10 javascript reactjs react-router next.js
我是 NextJs 新手,我试图通过组件将对象传递到另一个页面。我将发布代码并更好地解释我试图做的事情:
对象是这样的:
objectEx = {
name: 'name',
description: 'description'
}
Run Code Online (Sandbox Code Playgroud)
这是主要组件: 我试图在 Router.push 中传递对象
export default class ComponentDummy extends Component {
handleSubmit = value => e => {
e.preventDefault()
Router.push({
pathname: '/OtherPage'
query: { **PASS AN OBJECT HERE** } //what im trying is: query: { objectEx: objectEx},
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试图在查询中接收对象的页面
const OtherPage = ({ query }) => {
return(
<div>
Do Stuff with the object here
</div>
)
}
OtherPage.getInitialProps = ({ query }) => {
return { query }
}
Run Code Online (Sandbox Code Playgroud)
然后在上面的页面上,我尝试访问对象,例如:
query.objectEx.name
Run Code Online (Sandbox Code Playgroud)
这并不像我想象的那样工作。我怎样才能做到这一点?
提前致谢
Well, first thing is you passed object as a query param.
Router.push({
pathname: '/OtherPage'
query: { data: objectEx}
}
So, in order to access the object in the OtherPage, you need to fetch this inside componentDidMount.
componentDidMount() {
let data = window.location.search;
console.log(data)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2824 次 |
| 最近记录: |