如何将路线道具传递到我的功能组件中

时间:2020-01-27 22:16:07

标签: reactjs routes router

链接组件

   return (
                      <div key={driver.id}>
                        <Link to={"/drivers/" + driver.id}>
                          <OurStaffList driver={driver} />
                        </Link>
                      </div>

App.js

   <Switch>
      <Route exact path="/" component={HomePage} />
      <Route path="/archived-routes" component={ArchivedRoutes} />
      <Route path="/find-routes" component={FindRoutes} />
      <Route path="/signup" component={SignUp} />
      <Route path="/signin" component={SignIn} />
      <Route path="/drivers/:driver_id" component={DriverProfile} />
    </Switch>

现在在这里我需要路径ID的组件中,即时通讯收到“无法识别错误的参数...” 有人可以告诉我如何将路线道具放入组件中,以便呈现正确的驾驶员资料吗?

  const DriverProfile = ({ driver, getDrivers }) => {
  useEffect(() => {
    getDrivers();

    // eslint-disable-next-line
  }, []);
  console.log();
  return (
    <div className="col s12">
      <ul className="with-header">
        {driver.drivers &&
          driver.drivers.map(driver => {
            return <DriverProfileList driver={driver} key={driver.id} />;
          })}
      </ul>
    </div>
  );
};

DriverProfile.propTypes = {
  driver: PropTypes.object.isRequired
};

const mapStateToProps = (state, ownProps) => {
  let id = ownProps.match.params.driver_id;
  console.log(id);
  return {
    driver: state.driver
  };
};

2 个答案:

答案 0 :(得分:4)

https://reacttraining.com/react-router/core/api/Hooks/useparams

因此,在DriverProfile函数组件中:

google-cloud-storage
requests==2.20.0
requests-toolbelt==0.9.1

答案 1 :(得分:-1)

您可以使用https://reacttraining.com/react-router/core/api/withRouter

来包装组件中的所有路由参数