gpt4 book ai didi

reactjs - 如何将路由 Prop 传递到我的功能组件中

转载 作者:行者123 更新时间:2023-12-02 03:01:07 25 4
gpt4 key购买 nike

链接组件

   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 的组件中,我收到“错误无法识别参数...”有人可以告诉我如何将路线 Prop 放入组件中,以便我可以渲染正确的驾驶员配置文件吗?

  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
};
};

由用户6136000回答。谢谢

最佳答案

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

因此,在您的 DriverProfile 函数组件中:

let { driver_id } = useParams();

关于reactjs - 如何将路由 Prop 传递到我的功能组件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59939674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com