将对象传递给 Router.push NextJs 上的查询

Pass object to query on Router.push NextJs

我是 NextJs 的新手,我正在尝试通过组件将对象传递到另一个页面。我将 post 代码并更好地解释我要做什么:

对象是这样的:

objectEx = {
  name: 'name',
  description: 'description'
}

这是主要成分: 我试图在 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},
    }
  }
}

这是我试图在查询中接收对象的页面

const OtherPage = ({ query }) => {
  return( 
    <div> 
      Do Stuff with the object here
    </div>
  )
}

OtherPage.getInitialProps = ({ query }) => {
  return { query }
}

然后在上面的页面上,我试图访问对象,如:

query.objectEx.name

这并没有像我想的那样工作。我怎样才能做到这一点?

提前致谢

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)
}