Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass object as params in Router.push() in nextjs and access that object in other component

Router.push({pathname: '/card', query:{data: ObjectData}})

I get query as empty value could not pass object into it

like image 317
Poornima Shinde Avatar asked Jul 20 '20 12:07

Poornima Shinde


People also ask

How do you access parameters in NextJs?

You can access it inside the <Items> component like this. import React, { Component } from "react"; import { withRouter } from "next/router" class Items extends Component { render() { const { query } = this.

How do I transfer data to my next router?

There is no way to pass data between pages with Next's router. You would have to either use query strings as you mentioned, sessionStorage, or a state management option like Redux or React's Context API. You could then put that in your pages/_app.

How do I pass props from one page to another in next JS?

Props are passed from a parent component to its child components. What you are asking is to pass props between sibling components (ie. page) which I don't think has a straightforward way to do so. Maybe you can try using React's Context API so that your Test2 is not using any hard-coded value.


1 Answers

you can pass your params like this case:

router.push({
      pathname: '/card',
      query: { data: JSON.stringify(ObjectData)}
  })

and you can get params in card component like this:

JSON.parse(props.router.query.data);
like image 68
Medisa Avatar answered Oct 26 '22 18:10

Medisa