3

I've been struggling to route through pages using next js route query method. I was passing raw array of objects but it became empty in the page. I've been researching and I found a solution to use JSON.stringify(result) and parsing it in the other page with JSON.parse(query.result) and this worked. but when I use this approach on page reload the page crashes and the browser displays the following error.

This page isn’t workingIf the problem continues, contact the site owner.
HTTP ERROR 431

my code in the index.js is

<Link href={{
  pathname: "/TvShows",
  query: {
    result: JSON.stringify(result),
    //result: [result],
    img: img,
    index: index,
  }}}
  //as={"/TvShows"}
>

and the target page is Tvshows.js

first I defined a constants

const [result, setResult] = useState([]);
const [index, setIndex] = useState("");
const [img, setImg] = useState("");
const router = useRouter();
const query = router.query;

then

useEffect (()=>{
  if(router.isReady){
    //console.log(result);
    const res = JSON.parse(query.result);
    setResult(res);
    setIndex(query.index);
    setImg(query.img);
    //console.log(res);
    //router.isReady= false;
  }
},[router.isReady])

the problem is when I stringily and parse these JSON. why is this happening?

and NOTE: in the url section of the page it uses the datas and it is super long.(maybe if that has an effect on it somehow). what is the best way to pass an array to a page?

the local data being passed in JSON.strimgify(result)

const Shows = [
   {
     id: 1,
     title: "title1",
     img: "imgURL",
     desc: "ddescription1"
   },
   {
     id: 2,
     title: "title1",
     img: "imgURL",
     desc: "ddescription1"
   },
   {
     id: 3,
     title: "title1",
     img: "imgURL",
     desc: "ddescription1"
   },
   {
     id: 4,
     title: "title1",
     img: "imgURL",
     desc: "ddescription1"
   },
] export default Shows;
mobix
  • 465
  • 4
  • 10
  • 1
    I don't really understand why are you passing data this way? why not have a dynamic route like `/tvshow/[id]` and use `getStaticProps` with `getStaticPaths` ? – mocherfaoui Aug 11 '22 at 21:38
  • @mocherfaoui I want to pass data into it. specifically objects – mobix Aug 11 '22 at 22:07
  • why not go with a dynamic route? there you can get the data of each tvshow and it would be better that passing the data in the URL – mocherfaoui Aug 11 '22 at 22:19
  • @mocherfaoui can I pass dynamic in data to getStaticProps? – mobix Aug 11 '22 at 22:31
  • when you use a dynamic route you can get the data of that specefic page using different ways, including `getStaticProps`. this is covered in the documentation, if you can spin up a codesandbox I can help you – mocherfaoui Aug 11 '22 at 22:40
  • @mocherfaoui i would appreciate it. – mobix Aug 11 '22 at 22:46
  • could be the url has some illegal characters, did you try using encodeURIComponent? or the url is too long. personally i would request the data again on the new page instead of passing it with router like this – Chris Li Aug 11 '22 at 23:03
  • @ChrisLi the url is passed from an object for a YouTube playlist. I'm fetching YouTube playlist and when selected a playlist card it routes with all playlist data comprised into JSON object – mobix Aug 11 '22 at 23:06
  • is there any reason you cant fetch the same playlist on the new page? – Chris Li Aug 12 '22 at 01:02
  • @ChrisLi b/c I couldn't find a way to pass in the selected playlist id props to `getStaticProps`. – mobix Aug 12 '22 at 07:21
  • you didn't create a codesandbox? – mocherfaoui Aug 12 '22 at 07:58
  • @mocherfaoui I did create – mobix Aug 12 '22 at 08:03
  • @mocherfaoui but in my `index.js` I did it with `getStaticProps`, that's how I get the playlist detail. but how can I pass in props to `getStaticProps` so that I can create it in my `TvShows.js`? – mobix Aug 12 '22 at 08:06
  • can you post the link of the codesandbox? – mocherfaoui Aug 12 '22 at 08:37
  • @mocherfaoui https://codesandbox.io/s/competent-hoover-wizxms – mobix Aug 12 '22 at 09:08
  • but this is an empty codesandbox? you should add the pages and the logic you were trying to implement and I'll go from there – mocherfaoui Aug 12 '22 at 09:18
  • @mocherfaoui that link expired so https://codesandbox.io/live/6c2247c273f – mobix Aug 12 '22 at 09:53
  • here https://codesandbox.io/s/condescending-matsumoto-qx2ot3 hopefully this solves your issue – mocherfaoui Aug 12 '22 at 10:50
  • @mocherfaoui how about the playlist object. that's the issue I had – mobix Aug 12 '22 at 10:56
  • what playlist object? I didn't see any in your post or the codesandbox? – mocherfaoui Aug 12 '22 at 11:28
  • @mocherfaoui in the second link – mobix Aug 12 '22 at 11:51
  • @mocherfaoui if there is a way for me to pass playlistId to `getStaticProps` or pass an array in `staticPaths` that would solve everything. – mobix Aug 12 '22 at 11:53

1 Answers1

-1

Interestingly, I couldn't use the word 'code' as a query parameter in my project, so I renamed it to codeTerm. First, I suggest you to stay away some keywords like 'index' or 'code' etc.

In your TvShows component, try to console query object like below

import { withRouter } from 'next/router';

const TvShows = (props) => {
  // the rest of your code
  useEffect(() => {
    console.log(props.router.query);
    if (props.router.query.result)
      console.log(JSON.parse(props.router.query.result));
  }, [props.router]);
  // the rest of your code
}

export default withRouter(TvShows);

You do not need that

const router = useRouter();
const query = router.query;

UPDATE

Can you try to route user using next/router instead of next/link?

import Router from 'next/router';

Router.push(
  {
    pathname: '/TvShows',
    query: {
      result: JSON.stringify(result),
      img: img,
      index: index,
    },
  }
);

This should give you what you want, here's the result

Parsed

Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22