【发布时间】:2018-10-18 12:24:33
【问题描述】:
我知道如何在反应路由器中传递道具,例如 string 类型。但是当我尝试传递函数道具时遇到问题。在我的孩子组件上,这个道具是"undefined"。
我的链接示例:
<Link to={'/Content/' + this.props.index + '/' + this.props.decreaseIndexProject}>Page n°1</Link>
索引道具是一个数字,所以我可以在我的子组件中获取它,但不是decreaseIndexProject 道具。
我使用 PropType :
NavBar.propTypes = {
indexProject: PropTypes.number,
decreaseIndexProject: PropTypes.func
};
我的路由器组件:
<Router>
<Switch>
<Route path="/Content/:index/:decrease" exact name="content" component={Content} />
</Switch>
</Router>
也许还有其他方法可以传递函数?感谢您的帮助。
【问题讨论】:
-
你将一个函数连接成一个字符串?我不明白你要做什么
-
嗯,我不这么认为,我只是在路由器上传递了两个不同类型的参数,不是吗?
-
不,您正在尝试将
index和decreaseIndexProject连接到字符串'/Content/',如果decreaseIndexProject是一个函数,这是不可能的。
标签: javascript reactjs function react-router-v4 react-props