Skip to content
Advertisement

react-router – pass props to handler component

I have the following structure for my React.js application using React Router:

JavaScript

I want to pass some properties into the Comments component.

(normally I’d do this like <Comments myprop="value" />)

What’s the easiest and right way to do so with React Router?

Advertisement

Answer

UPDATE

Since new release, it’s possible to pass props directly via the Route component, without using a Wrapper. For example, by using render prop.

Component:

JavaScript

Usage:

JavaScript

Codesandbox Example


OLD VERSION

My preferred way is wrap the Comments component and pass the wrapper as a route handler.

This is your example with changes applied:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement