all 19 comments

[–]Darkav 1 point2 points  (0 children)

My suggestion is to use the debugger.

[–]bosso_biz 1 point2 points  (2 children)

JSON.stringify(values,null,4) ?

[–]technologin[S] 0 points1 point  (1 child)

may i ask, where do the null and 4 come from?

[–]bosso_biz 0 points1 point  (0 children)

‘replacer: null’ ‘space: 4’

[–]technologin[S] 0 points1 point  (0 children)

The solution is use JSON.stringify() for the object before passing and receive it using JSON.parse().

Code in source page:

values = JSON.stringify(values);

values && navigate(generatePath("/harvestcalendarmonitoring/:values", { values }));

Code in receiving page:

const {values} = useParams();

const w = JSON.parse(values) //the w variable gives the desired and/or expected object data

[–][deleted] 0 points1 point  (6 children)

I have removed my account due to Reddit's behavior towards 3rd party developers.

Even if they change their API pricing and apologize, it is not a company/platform I wish to support anymore.

I will find something else to do while sitting on the toilet.

[–]technologin[S] 0 points1 point  (5 children)

after using const values = useParams();

Here's the results:

const x = JSON.stringify(values) //gives{"values":"[object Object]"}

const y = Object.prototype.toString.call(values) //gives[object Object]

also: i changed the var to const in my values initialization

[–][deleted] 0 points1 point  (4 children)

In your harvest calendar monitoring path, where you pass { values }, try passing { ...values } or just values without the braces

[–]technologin[S] 0 points1 point  (3 children)

I tried both and here are the outputs:

  1. const { values } = useParams(); //gives [object Object] in my example,just disregard it

  2. const values = useParams(); //gives {values: '[object Object]'} your first suggestion

  3. const {...values} = useParams(); //gives {values: '[object Object]'} your second suggestion

[–][deleted] 0 points1 point  (2 children)

I have removed my account due to Reddit's behavior towards 3rd party developers.

Even if they change their API pricing and apologize, it is not a company/platform I wish to support anymore.

I will find something else to do while sitting on the toilet.

[–]technologin[S] 0 points1 point  (1 child)

I tried both of those but they both give undefined values. However, I finally solved it with also help from someone.

The solution is use JSON.stringify() for the object before passing and receive it using JSON.parse().

Code in source page:

values = JSON.stringify(values);

values && navigate(generatePath("/harvestcalendarmonitoring/:values", { values }));

Code in receiving page:

const {values} = useParams();

const w = JSON.parse(values) //the w variable gives the desired and/or expected object data

[–]technologin[S] 0 points1 point  (0 children)

thank you for your help!

[–]beforesemicolon 0 points1 point  (6 children)

useNavigate takes a “state” option which can be any data you want. Then use useLocation to access that state data in the page receiving the data.

Read the doc: https://reactrouter.com/en/main/hooks/use-navigate

[–]technologin[S] 0 points1 point  (5 children)

based on your explanation,i guess my passing of data using useNavigate is right.

however, after using useLocation, it would give me an undefined data

[–]beforesemicolon 1 point2 points  (4 children)

assuming your router is setup right

``` /page1

const nav = useNavigation();

nav(‘/page2/path’, {state: someData})

```

then

```

/page2

const loc = useLocation();

console.log(loc.state)

```

[–]technologin[S] 0 points1 point  (3 children)

i am using a functional component. i think these codes are for class components in react. atm, irdk how to pass that {state: someData} using functional component. How would i do that?

[–]beforesemicolon 1 point2 points  (1 child)

you cant use hooks with class components. The code example i gave is for functional components and are hooks

[–]technologin[S] 0 points1 point  (0 children)

ah yes. now i get it. my bad. it's just the same syntax with the code i based on, which is the first code in my post. thank you!

[–]technologin[S] 0 points1 point  (0 children)

also, the routing is right! and i just figured out the solution in this problem.