I want to use an event handler to redirect a user to another route in reactJS but I have not figured out how i can pass down it's props to the event handler function.
<button onClick={ChallengeActions.OpenChallenge(item.title) }>Start<button>
This is what my event listener looks like
onOpenChallenge: function(url) {
window.location.href = url;
}
The problem is that the function automatically fires up whenever i load the page instead of waiting for the event handler, I also get the following error.
Uncaught Error: Invariant Violation: Expected onClick listener to be a function, instead got type object
You need to take care of the following:
<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')
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.