Reputation:
Following is my code snippet
Child Class function (handleClick)
import React, { Component } from 'react';
import {render} from 'react-dom';
export class Badge extends Component {
handleClick() {
alert("this and that")
}
render() {
return (
<button whenClicked={this.handleClick} className={"btn " + this.props.className} type="button">
{this.props.title} <span className={this.props.subTitleClassName}>{this.props.subTitle}</span>
</button>
);
}
}
Parent Class
export class Dropdown extends Component {
render() {
return (
<div className="dropdown">
<Badge onClicked={this.props.whenClicked} className="btn btn-default" title={this.props.title} subTitleClassName="caret" />
</div>
);
}
}
In above code whenever I try to call handleClick function in child class it is not working.
Upvotes: 2
Views: 4729
Reputation: 27174
As pointed out by others, the correct name for the click event attribute is onClick
(not whenClick
).
Badge (Child):
import React, { Component } from 'react';
import {render} from 'react-dom';
export class Badge extends Component {
render() {
return (
<button onClick={this.props.whenClicked} className={"btn " + this.props.className} type="button">
{this.props.title} <span className={this.props.subTitleClassName}>{this.props.subTitle}</span>
</button>
);
}
}
Which leads me to wonder: Where is your this.props.whenClicked
defined, which you want to pass from Dropdown
into Badge
?
I imagine you want to define it in Dropdown
as well and pass it in as this.whenClicked
.
Dropdown (Parent):
export class Dropdown extends Component {
whenClicked(event) {
// Your event handler
},
render() {
return (
<div className="dropdown">
<Badge whenClicked={this.whenClicked} className="btn btn-default" title={this.props.title} subTitleClassName="caret" />
</div>
);
}
}
To recap:
whenClicked
is defined in Dropdown
and passed as a property into the Dropdown
instance..onClick
listener called handleClick
which calls the whenClicked
that you passed from Dropdown.Upvotes: 2
Reputation: 98
You got parent and child mixed up. The Dropdown component renders the Badge component, so Dropdown is the parent class.
Also the Event handler is called onClick
.
Here's the code (in ES5):
Dropdown
var React = require('react');
var Badge = require('./Badge.react');
var Dropdown = React.createClass({
whenClicked: function() {
console.log('blabla');
},
render: function () {
return (
<div className="dropdown">
<Badge whenClicked={this.whenClicked} className="btn btn-default" title={this.props.title} subTitleClassName="caret"
subTitle="subTitle" />
</div>
);
}
});
module.exports = Dropdown;
Badge
var React = require('react');
var Badge = React.createClass({
render: function () {
return (
<button onClick={this.props.whenClicked} className={"btn " + this.props.className} type="button">
{this.props.title} <span className={this.props.subTitleClassName}>{this.props.subTitle}</span>
</button>
);
}
});
module.exports = Badge;
And in ES6:
Badge
import React from 'react';
class Badge extends React.Component {
handleClick() {
this.props.whenClicked();
}
render() {
return (
<button onClick={this.props.whenClicked} className={"btn " + this.props.className} type="button">
{this.props.title} <span className={this.props.subTitleClassName}>{this.props.subTitle}</span>
</button>
);
}
}
module.exports = Badge;
Dropdown
import React from 'react';
import Badge from './Badge.react';
class Dropdown extends React.Component {
whenClicked() {
console.log('blabla');
}
render() {
return (
<div className="dropdown">
<Badge whenClicked={this.whenClicked} className="btn btn-default"
title={this.props.title} subTitleClassName="caret" subTitle="subTitle"/>
</div>
);
}
}
module.exports = Dropdown;
Upvotes: 3
Reputation: 5663
Upvotes: 0