[英]Props not being passed to relative url in ReactJS/react-router
I have the following code, where in {...props}
a function is being passed down from a parent component. 我有以下代码,其中在{...props}
一个函数是从父组件传递下来的。 In all the routes, this function is available in props apart from those routes that have a /:id
. 在所有路线中,除了具有/:id
的路线之外,该功能在props中都可用。
Any ideas why? 有什么想法吗?
<Switch>
<Route exact="exact" path="/" render={() => (<ManageClients {...props}/>)}/>
<Route exact="exact" path="/new-client" render={() => (<AddClient {...props}/>) }/>
<Route exact="exact" path="/admins" render={() => (<ManageAdmins {...props}/>) }/>
<Route exact="exact" path="/files" render={() => (<ManageFiles {...props}/>) }/>
<Route exact="exact" path="/new-file" render={() => (<AddFile {...props}/>) }/>
<Route exact="exact" path="/update-client/:id" render={(props) => (<EditClient {...props} />) }/>
<Route exact="exact" path="/update-file/:id" render={(props) => (<EditFile {...props}/>) }/>
<Route exact="exact" path="/clone/:id" render={(props) => (<CloneClient {...props}/>) }/>
<Route exact="exact" path="/new-admin"render={() => (<NewAdmin {...props}/>) }/> </Switch>
Use this if you want to pass the parent props and the routes props separately 如果要分别传递父项道具和路线道具,请使用此选项
<Route exact="exact" path="/clone/:id" render={(otherProps) => <CloneClient {...props} otherProps={otherProps} /> }/>
or this if you want to merge them: 如果要合并它们,请执行以下操作:
<Route exact="exact" path="/clone/:id" render={(otherProps) => <CloneClient {...props, ...otherProps} /> }/>
The above answer was almost there. 上面的答案几乎就在那里。 I had to remove the <Switch>
around my routes, and then write the route as: 我必须删除路由周围的<Switch>
,然后将路由写为:
<Route
exact="exact"
path="/update-client/:id"
render={otherProps => (
<EditClient {...props} {...otherProps} />
)}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.