Javascript 如何从reactjs中的按钮向函数传递道具

Javascript 如何从reactjs中的按钮向函数传递道具,javascript,reactjs,Javascript,Reactjs,我想在reactJS中使用事件处理程序将用户重定向到另一个路由,但我还没有弄清楚如何将它的道具传递给事件处理程序函数 <button onClick={ChallengeActions.OpenChallenge(item.title) }>Start<button> 问题是,每当我加载页面而不是等待事件处理程序时,函数就会自动启动,我还得到以下错误 Uncaught Error: Invariant Violation: Expected onClick listen

我想在reactJS中使用事件处理程序将用户重定向到另一个路由,但我还没有弄清楚如何将它的道具传递给事件处理程序函数

<button onClick={ChallengeActions.OpenChallenge(item.title) }>Start<button>
问题是,每当我加载页面而不是等待事件处理程序时,函数就会自动启动,我还得到以下错误

Uncaught Error: Invariant Violation: Expected onClick listener to be a function, instead got type object
var ChildComponent=React.createClass({
渲染:函数(){
var myCustomValue=“测试这个”;
返回(
测试子点击功能
)
}
});
var ParentComponent=React.createClass({
customEvent:函数(值){
//做有价值的事
警报(“您通过了”+值);
},
渲染:函数(){
返回(
)
}
});
反应(
,
document.getElementById('示例')
)

您需要注意以下几点:

  • 事件处理程序需要映射到函数:

  • 我明白这一点,但假设我想向事件传递一个值,我该怎么做呢?我更新了我的答案,提供了一个完整的示例,说明如何在事件期间将值从孩子传递给家长。谢谢,我实际上决定改为使用链接,但我也需要这个链接。将测试并评论其运行方式
    Uncaught Error: Invariant Violation: Expected onClick listener to be a function, instead got type object
    
    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')
    )
    
    <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;
    }