简体   繁体   English

如何在 React Components 中正确传递 props?

[英]How to pass props in React Components correctly?

I have routes array, which pass into RouteComponent我有路由数组,它传递给 RouteComponent

const info = [
  { path: '/restaurants/:id', component: <Restaurant match={{ params: '' }} /> },
  { path: '/restaurants', component: <ListRestaurant match={{ path: '/restaurants' }} /> }
];

I use Axios for connection with back-end我使用 Axios 与后端连接

Restaurant Component:餐厅组成:

  async componentDidMount() {
    this.getOne();
  }
  getOne() {
    const { match } = this.props;
    Api.getOne('restaurants', match.params.id)

Restaurant Component:餐厅组成:

When I see console there is en error like this当我看到控制台时,会出现这样的错误在此处输入图片说明

So, what can be passed as the props?那么,什么可以作为道具传递呢? Can't find solution.找不到解决办法。 Thanks in advance提前致谢

App.js应用程序.js

import ...
import info from './components/info/routes';

class App extends Component {
  render() {
    const routeLinks = info.map((e) => (
      <RouteComponent
        path={e.path}
        component={e.component}
        key={e.path}
      />
    ));
    return (
      <Router>
        <Switch>
          {routeLinks}
        </Switch>
      </Router>
    );
  }
}

RouteComponent.js路由组件.js

import { Route } from 'react-router-dom';

class RouteComponent extends Component {
  render() {
    const { path, component } = this.props;
    return (
      <Route path={path}>
        {component}
      </Route>
    );
  }
}

RouteComponent.propTypes = {
  path: PropTypes.string.isRequired,
  component: PropTypes.object.isRequired,
};

EDITED 22/03/2020编辑 22/03/2020

Line with线与gives this: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.给出了这个:错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:对象。

Check the render method of Context.Consumer .检查Context.Consumer的渲染方法。

class RouteComponent extends Component {
  render() {
    const { path, component } = this.props;
    return (
      <Route path={path} component={component} />
    );
  }
}

RouteComponent.propTypes = {
  path: PropTypes.any.isRequired,
  component: PropTypes.any.isRequired,
};

But as you see, I make PropTypes 'any'但如你所见,我将 PropTypes 设为“任何”

Ok there are some changes you will need to do and might not be enough.好的,您需要进行一些更改,但可能还不够。 So let me know if does not work in comments.所以如果在评论中不起作用,请告诉我。

Step one步骤1

send component correctly to routes将组件正确发送到路由

class RouteComponent extends Component {
  render() {
    const { path, component } = this.props;
    return (
      <Route path={path} component={component}/>
    );
  }
}

Step two第二步

Send JSX element, not JSX object发送 JSX 元素,而不是 JSX 对象

const info = [
  { path: '/restaurants/:id', component: Restaurant },
  { path: '/restaurants', component: ListRestaurant }
];

Import RouteProps from react-router-dom and use it as the interface for props in routecomponent.js.从 react-router-dom 导入 RouteProps 并将其用作 routecomponent.js 中 props 的接口。

Then, instead of calling component via expression, call it like a component, ie然后,不是通过表达式调用组件,而是像组件一样调用它,即

Eg,例如,

function Routecomponent({ component: Component, ...rest }: RouteProps) {
    if (!Component) return null;

    return (
        <Route
            {...rest}
                <Component {...props} />
               />}
    )
}

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

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