Yaswanth
Yaswanth

Reputation: 58

passing props to component during routing

import React from 'react';
import SearchDocument from './components/searchDocument';
import CreateRequest from './components/createRequest';
import { BrowserRouter as Router } from "react-router-dom";
import { Route, Switch } from 'react-router-dom';
class App extends React.Component {
    render() {
        return (
            <Router>
                <div>
                    <Switch>
                        <Route exact path='/' component={Home} />
                        <Route path='/home' component={Home} />
                        <Route path='/create' component={CreateRequest} />
                        <Route path='/searchDocument' component{SearchDocument} />
                        <Route path='/upload' component={UploadDocument} />
                        <Route path='/search' component={Search} />
                    </Switch>
                </div>
            </Router>
        );
    }
}
export default App;

I am new to react router.I want to pass props through route to create request component.I tried different methods to pass props but that doesn't work.Can anyone please suggest, how to send props to component and how to handle them in that component.Above is my code.Thanks in advance

Upvotes: 1

Views: 65

Answers (1)

Matan Bobi
Matan Bobi

Reputation: 2813

As mentioned in the comments, you can use the render prop instead of the component.

From the docs:

Instead of having a new React element created for you using the component prop, you can pass in a function to be called when the location matches. The render prop receives all the same route props as the component render prop.

And an example with your code will look like this:

<Route path='/searchDocument' render={(props) => <SearchDocument yourNewProp={yourNewProp} {...props} />} />

Upvotes: 1

Related Questions