I have my routes defined and my links working fine. I would like to pass a prop through the link when the component is called by the router. I dont know if that is even possible.
For better understanding please check in the code:
// I WANT TO PASS THE CURRENT PROJECT AS A PROP HERE
and //I WANT TO GET THE PASSED PROP IN THE LINK HERE
My functional component in react:
const ProjectList = () => {
....//not meaningful code. I get my data from a call to a database so its not passed in as an argument
return(
<div className="project-list section">
{ projects && projects.map( project => {
const path = '/project/' + project.id;
return (
<Link to={path} key={project.id}> // I WANT TO PASS THE CURRENT PROJECT AS A PROP HERE
<ProjectSummary project={project} deleteCallback={projectDelete}/>
</Link>
)
})}
</div>
)
}
My routes setup (imports omitted):
function App() {
return (
<BrowserRouter>
<div className="App">
<NavBar />
<Switch>
<Route exact path='/' component={Dashboard}/>
<Route
path='/project/:id'
render={(props) => (
<ProjectDetails {...props}/> //I WANT TO GET THE PASSED PROP IN THE LINK HERE
)}
/>
<Route path='/signin' component={SignIn}/>
<Route path='/signup' component={SignUp}/>
<Route path='/create' component={CreateProject}/>
</Switch>
</div>
</BrowserRouter>
);
}
export default App;
This is to get the property from a functional component, called when the user clicks the link so called by the Router. This is where I would pass the prop in, on the Router call.
Not very relevant, but to check the name of the according Route child component from the code above, it would be a:
const ProjectDetails = (project: IFirebaseProject) => {
//....not meaningfull code
}
I checked approximations as:
<Link to="ideas" params={{ testvalue: "hello" }}>Create Idea</Link>
or:
<Link to={{
pathname: '/tylermcginnis',
state: {
fromNotifications: true
}
}}>Tyler McGinnis</Link>
This last one is very similar to what I would like to do, this is pass a state or an object with properties through, however I am not able to make it work.
Other attempts got from here
Relevant versions info from the package.json:
"typescript": "^4.1.3",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-redux": "^7.2.2",
"react-router-dom": "^5.2.0",