未将道具传递到ReactJS/react路由器中的相对url
我有以下代码,其中在未将道具传递到ReactJS/react路由器中的相对url,reactjs,react-router,Reactjs,React Router,我有以下代码,其中在{…props}中,一个函数正在从父组件传递下来。在所有路线中,除了具有/:id的路线外,该功能在道具中可用 你知道为什么吗 <Switch> <Route exact="exact" path="/" render={() => (<ManageClients {...props}/>)}/> <Route exact="exact" path="/new-client" render={() => (<A
{…props}
中,一个函数正在从父组件传递下来。在所有路线中,除了具有/:id
的路线外,该功能在道具中可用
你知道为什么吗
<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>
()}/>
() }/>
() }/>
() }/>
() }/>
() }/>
() }/>
() }/>
() }/>
如果要分别传递父道具和路线道具,请使用此选项
}/>
如果要合并它们,请执行以下操作:
}/>
上面的答案几乎就在那里。我必须删除路线周围的
,然后将路线写为:
<Route
exact="exact"
path="/update-client/:id"
render={otherProps => (
<EditClient {...props} {...otherProps} />
)}
/>
(
)}
/>
当你有:id时,你在做(道具)=>()
当你没有它时,你在做()=>()
。移除道具作为输入参数第一种情况下,它应该以同样的方式工作?是的,我已经尝试过了。问题是,我必须在第一个参数中传入props,才能访问这些特定组件中的this.props.match.params
。没有它,这些组件就无法工作,因为我的函数无法访问参数。我相应地更新了答案。当路由加载时,这仍然返回无法读取未定义的属性'params'(如果使用otherProps={otherProps},您使用了哪一个?)
您应该使用this.props.otherProps.match.params
。