I was wondering if it is possible to pass an object as a parameter to a react-router-dom endpoint using react-router-dom-v6. I've seen some ways of doing it with Hooks but they require functions instead of components.
This is the main component handling the Routes
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { Home } from "./Home";
import { Page01 } from "./Page01";
import { Page02 } from "./Page02";
type TypeProps = { name:string }
type TypeState = { }
class Main extends React.Component<TypeProps, TypeState> {
constructor(props:TypeProps) {
super(props);
this.state = { };
}
render() {
return (
<div>
<BrowserRouter>
<Routes>
<Route path="/" element={ <Home name="home"/> }/>
<Route path="/page01" element={ <Page01 name="page01"/> }/>
<Route path="/page02" element={ <Page02 name="page02"/> }/>
</Routes>
</BrowserRouter>
</div>
);
}
}
ReactDOM.render(<Main name="main" />, document.getElementById("main"));
Then a basic component which is only handling the Link to method.
import React from "react";
import ReactDOM from "react-dom";
import { Link } from "react-router-dom";
type TypeProps = { name:string }
type TypeState = { }
class Page01 extends React.Component<TypeProps, TypeState> {
constructor(props:TypeProps) {
super(props);
this.state = { };
}
render() {
return (
<div>
<Link to={ "/page02" }>
<span>Go To Page 02</span>
</Link>
</div>
);
}
}
export { Page01 }
And then an almost identical component at the other end which will need to receive this data.
import React from "react";
import ReactDOM from "react-dom";
import { Link } from "react-router-dom";
type TypeProps = { name:string }
type TypeState = { }
class Page02 extends React.Component<TypeProps, TypeState> {
constructor(props:TypeProps) {
super(props);
this.state = { };
}
render() {
return (
<div>
<Link to={ "/page01" }>
<span>Go To Page 01</span>
</Link>
</div>
);
}
}
export { Page02 }
I've tried to pass the data with state
let paramData = {
field01:"data01",
field02:"data02"
}
<Link to={ "/page02" } state={{ paramData:paramData }}>
<span>Go To Page 02</span>
</Link>
But I cannot receive it on the other end because the I'm using a component and not a function