简体   繁体   English

如何将props从react-router Switch语句传递到组件

[英]How to pass props into component from react-router Switch statement

I have a simple array with the possible routes in it, and then map them inside a react-dom-router <Switch> component to show the proper route that the user is on. 我有一个包含可能路由的简单数组,然后map它们mapreact-dom-router <Switch>组件中,以显示用户所在的正确路由。

The question I have is: How do I pass a prop from the route object to the component being used within the <Switch> ? 我的问题是:如何将prop从路由对象传递到<Switch>使用的组件? The key in question here is the someKey prop in the /faq route. 这里的关键是/faq路由中的someKey道具。

route.js route.js

import Home from "../components/home/Home";
import FaqPage from "../components/faq/FaqPage";

export default [
    {
        path: "/",
        exact: true,
        component: Home
    },
    {
        path: "/faq",
        component: FaqPage,
        someKey: "Test"
    }
];

Body.js Body.js

import React, { Component } from "react";
import { Switch, Route } from "react-router-dom";

// Data
import routes from "../shared/routes";

class Body extends Component {
    render() {
        return (
            <div className="main-body">
                <Switch>
                    {routes.map((route, i) => {
                        // HOW DO I PASS THE 'someKey' PROP HERE?
                        return <Route key={i} {...route} />;
                    })}
                </Switch>
            </div>
        );
    }
}

export default Body;

I've tried a few different ways suggested but I've been unable to access the prop within the FaqPage component. 我尝试了几种建议的方法,但无法访问FaqPage组件中的道具。 Inside that component I tried to use the prop with a statement like this.props.someKey with no luck. 在该组件内部,我尝试将prop与诸如this.props.someKey这样的语句this.props.someKey使用,但没有运气。 Every sample I see uses a hardcoded component inside the <Route /> , but I'm using a variable. 我看到的每个样本都在<Route />内使用硬编码组件,但我使用的是变量。

https://github.com/ReactTraining/react-router/issues/4105#issuecomment-291834881 https://github.com/ReactTraining/react-router/issues/4627#issuecomment-332972317 https://github.com/ReactTraining/react-router/issues/4105#issuecomment-291834881 https://github.com/ReactTraining/react-router/issues/4627#issuecomment-332972317

Any idea what I should be doing to make this happen? 任何想法我应该怎么做才能做到这一点?

You could wrap component into render function. 您可以将组件包装到render函数中。

import React, { Component } from "react";
import { Switch, Route } from "react-router-dom";

// Data
import routes from "../shared/routes";

class Body extends Component {
    render() {
        return (
            <div className="main-body">
                <Switch>
                    {routes.map(({component: Cmp, ...route}, i) => {
                        // HOW DO I PASS THE 'someKey' PROP HERE?
                        return (<Route
                                 key={i}
                                 {...route}
                                 render={props => <Cmp {...props} someKey="someValue" />} 
                               />);
                    })}
                </Switch>
            </div>
        );
    }
}

export default Body;

Try adding a render function into your Route Component in Body.js and access the component there- 尝试将渲染功能添加到Body.js中的Route Component中,然后在其中访问该组件-

class Body extends Component {
    render() {
        return (
            <div className="main-body">
                <Switch>
                    {routes.map(({component: ComponentDetails, ...route}, i) => {
                        // HOW DO I PASS THE 'someKey' PROP HERE?
                        return (
                               <Route 
                                exact path='/'
                                key={i} 
                                {...route}
                                render={(props)=>
                                 <ComponentDetails {...props}
                                   someKey="TEST"  />)
                </Switch>
            </div>
        );
    }
}

export default Body;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM