I have a BoardHome.tsx component and I can't pass props (or state) to the Board.tsx component in the Link. I only get an empty object or null.
BoardHome.tsx
export default function BoardHome(props: { id: number; title: string }) {
//props are displayed correctly
console.log(props)
return (
<div className="board-home">
<Link
className="board-link"
to={{ pathname: '/board/' + props.id }}
state={{ id: props.id }}
>
<div className="board-fade">
<h2 className="board-title">{props.title}</h2>
</div>
</Link>
<Routes>
<Route path="/board/:id" element={<Board />} />
</Routes>
</div>
)
}
Board.tsx
import React from 'react'
import './board.scss'
import '../../index.css'
interface BoardState{
id: number
}
export default class Board extends React.Component<{ id?: number }, BoardState> {
render() {
//empty object
console.log(this.props)
//null
console.log(this.state)
return (
<div className="board-header">
<h1 className="board-h1">{this.state.id}</h1>
</div>
)
}
}
I don't know if it matters, but both components should be class components, not function components.
I tried different ways, but it still failed to pass the props.
Was reading:
How to pass data from a page to another page using react router How to pass props within components with react-router v6 how to pass props to react router v6 link How to pass props using react router link v6? React: how to pass props through a Link from react-router-dom? How to pass data from a page to another page using react router
all these answers are for functional components