如何从reactjs中的按钮向函数传递props
how to pass props to a function from a button in reactjs
我想在reactJS中使用事件处理程序将用户重定向到另一个路由,但我还没有弄清楚如何将其道具传递给事件处理程序函数。
<button onClick={ChallengeActions.OpenChallenge(item.title) }>Start<button>
这就是我的事件监听器的样子
onOpenChallenge: function(url) {
window.location.href = url;
}
问题是,每当我加载页面时,该函数就会自动启动,而不是等待事件处理程序,我还会得到以下错误。
Uncaught Error: Invariant Violation: Expected onClick listener to be a function, instead got type object
您需要注意以下事项:
- 事件处理程序需要映射到一个函数:
<button onClick={this.onChallengeButtonClicked}>Start</button>
- 您需要告诉每个按钮对应的项目:
<button item={item} onClick={this.onChallengeButtonClicked}>Start</button>
- 事件处理程序函数可以通过访问事件参数来检测单击了哪个按钮项:
onChallengeButtonClicked: function(event) {
item = event.currentTarget.item;
window.location.href = item.title;
}
var ChildComponent = React.createClass({
render: function () {
var myCustomValue = "testing this out";
return (
<h1 onClick={this.props.customEvent.bind(this, myCustomValue)}>Testing child click function</h1>
)
}
});
var ParentComponent = React.createClass({
customEvent: function (value) {
// do something wtih value
alert("you passed " + value);
},
render: function () {
return (
<ChildComponent customEvent={this.customEvent} />
)
}
});
React.render(
<ParentComponent customEvent={this.customEvent} />,
document.getElementById('example')
)
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 如何从reactjs中的按钮向函数传递props
- React.jsthis.props.data.map()不是函数
- ReactJS:this.props.data.map 不是一个函数
- React JS:用 props 传递我的处理程序函数时出现问题
- 未捕获类型错误:this.props.data.map不是函数(使用超级代理)
- React Redux Reducer: 'this.props.tasks.Map不是函数'错误
- this.props.enableEdit不是执行函数时的函数错误
- React:将props传递给函数组件
- 未捕获类型错误:this.props.onDelete不是函数reactjs
- 如何模拟函数,并在react组件中作为props传递时测试它们是否被调用
- 检查函数react redux组件中从mapStateToProps传递的props的真实性
- 何时需要使用super(props)将prop传递给react组件的构造函数
- 从子级调用回调函数时,React-props为空
- Uncaught TypeError: this.props.data.map不是一个函数
- 未捕获的类型错误:this.props.data.map不是函数
- 如何将props(状态和函数)传递给子路由器组件