简体   繁体   English

如何将自定义道具和历史传递给 Route

[英]How to pass custom props and history to Route

How to I get access to the history function when passing custom props to component.将自定义道具传递给组件时如何访问历史记录功能。

<Route path="/" exact component={<Home prop1={this.state.example}/>}/>

If I try getting the history object in Home component, I can't get to use the history functions.如果我尝试在 Home 组件中获取历史对象,我将无法使用历史函数。 I logged the history object in the console and I can see there's not push or replace functions anymore.我在控制台中记录了历史对象,我可以看到不再有pushreplace功能。

Where as, using this哪里,使用这个

<Route path="/" exact component={Home}/>

works perfecting except then I can't pass my custom props.作品完善,除非我无法通过我的自定义道具。

You just need to use the Spread operators and pass your custom props alongside.您只需要使用 Spread 运算符并将您的自定义道具一起传递。

 <Route exact path="/" component={props => <Home props={{ data: "check", ...props }}/>}/>

If you console the props inside Home component, you can see the value of data in props.data如果你在 Home 组件中控制台 props,你可以在 props.data 中看到 data 的值

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

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