React Router Pass Param for component

const rootEl = document.getElementById('root');

ReactDOM.render(
    <BrowserRouter>
        <Switch>
            <Route exact path="/">
                <MasterPage />
            </Route>
            <Route exact path="/details/:id" >
                <DetailsPage />
            </Route>
        </Switch>
    </BrowserRouter>,
    rootEl
);

I am trying to access the id in the DetailPage component, but it is not available. I tried

<DetailsPage foo={this.props}/>

pass parameters to DetailsPage, but in vain.

export default class DetailsPage extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div className="page">
            <Header />
            <div id="mainContentContainer" >

            </div>
            </div>
    );
    }
}

So, any idea on how to pass an identifier in a DetailsPage?

+40
source share
8 answers

If you want to pass the details to the component inside the route, the easiest way is to use render, for example:

<Route exact path="/details/:id" render={(props) => <DetailsPage globalStore={globalStore} {...props} /> } />

You can access the details inside DetailPageusing:

this.props.match
this.props.globalStore

{...props}you must pass the original details of Route, otherwise you will get this.props.globalStoreinside DetailPage.

+56
source

ID :

<Route path="/details/:id" component={DetailsPage}/>

:

export default class DetailsPage extends Component {
  render() {
    return(
      <div>
        <h2>{this.props.match.params.id}</h2>
      </div>
    )
  }
}

h2, , -.

+42

render:

<Route exact path="/details/:id" render={(props)=>{
    <DetailsPage id={props.match.params.id}/>
}} />

id :

this.props.id

DetailsPage

+15

:

<Route exact path="/details/:id" component={DetailsPage} />

id, :

this.props.match.params.id

DetailsPage

+5

to <Link />. new URLSearchParams();

<Link 
  key={id} 
  to={{
    pathname: this.props.match.url + '/' + foo,
    search: '?foo=' + foo
  }} />

<Route path="/details/:foo" component={DetailsPage}/>

export default class DetailsPage extends Component {

    state = {
        foo: ''
    }

    componentDidMount () {
        this.parseQueryParams();
    }

    componentDidUpdate() {
        this.parseQueryParams();
    }

    parseQueryParams () {
        const query = new URLSearchParams(this.props.location.search);
        for (let param of query.entries()) {
            if (this.state.foo!== param[1]) {
                this.setState({foo: param[1]});
            }
        }
    }

      render() {
        return(
          <div>
            <h2>{this.state.foo}</h2>
          </div>
        )
      }
    }
+4

... , :

<Route path="/details/:id/:title" component={DetailsPage}/>

export default class DetailsPage extends Component {
  render() {
    return(
      <div>
        <h2>{this.props.match.params.id}</h2>
        <h3>{this.props.match.params.title}</h3>
      </div>
    )
  }
}
+2

. "react-router-dom": "^4.3.1"

export const AppRouter: React.StatelessComponent = () => {
    return (
        <BrowserRouter>
            <Switch>
                <Route exact path="/problem/:problemId" render={props => <ProblemPage {...props.match.params} />} />
                <Route path="/" exact component={App} />
            </Switch>
        </BrowserRouter>
    );
};

export class ProblemPage extends React.Component<ProblemRouteTokens> {

    public render(): JSX.Element {
        return (<div>{this.props.problemId}</div>);
    }
}

ProblemRouteTokens

ProblemRouteTokens {     problemId: ; }

+1

- v5.1 :

import { useParams } from 'react-router';

export default function DetailsPage() {
  const { id } = useParams();
}

https://reacttraining.com/blog/react-router-v5-1/

0
source

Source: https://habr.com/ru/post/1684537/


All Articles