score:3
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;
score:0
- the dropdown class should define the action that will be called when badge is clicked.
- you need to pass a delegate to this function as a property to the badge. you can call the properly whatever you want.
- in the badge class you need to change the 'whenclicked' to onclick and pass the this.props.thenameofthefunction
score:2
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:
- your
whenclicked
is defined indropdown
and passed as a property into thedropdown
instance.. - in badge, you define a
onclick
listener calledhandleclick
which calls thewhenclicked
that you passed from dropdown.
Source: stackoverflow.com
Related Query
- How to call parent class function in child class in React JS
- How to call parent function from child prop React 15.5.0
- How to call function in parent class component by onchange event from child component in React?
- How to call child function from Parent react hook
- ReactJS: How to get a functional parent component to call a function in a child class component
- How to have a React child component call a function from a parent component
- How can I call parent function in a child React component?
- How can I call parent method in a child React component?
- How to call a parent function from a child - react-native
- how to call function of child in functional component from parent in react.js?
- how to set react parent component state by a async function and then pass this state to child as props?
- How can I call a Parent function from a React functional component?
- How to call inherited function in react child component
- React - parent class component function change class of child component
- call parent function from child of child component react
- How to call React/Typescript child component function from parent component?
- how to call parent function controller from directive react component angularjs + reactjs?
- How to call parent function in child component
- Call child method from parent class - React typescript
- React - How can I set my parent state using child component, using function structures (not classes)
- How to call child component's method from a parent component in React
- How to fire a setState function in react from child componenet to parent componenet with data from a button click
- Call child function in parent with i18next react
- how to pass in a value from child to parent class in react js
- Unable to call parent component function from child component in React
- How to call Parent containers function from child component in Reactjs
- React - call function in child component if parent state changes
- How to call Child function from Parent / Pass Props to a Parent component without using ref methods?
- How to call parent function from custom child component?
- how to call a function from child components to parent component(function is in parent) without including the full component
More Query from same tag
- Using React Browser history push gives strange error duplicate Components
- Can not find function in reactjs
- TypeError: Cannot read properties of null , don't catch data- React
- The this keyword is undefined in React base class
- Javascript Optional chaining doesnt work with template literals
- When to replace localhost with service name in docker?
- How to test that dispatch only gets called once
- Disable link on ant design form submit
- Error when trying to install react-twitter-embed
- bizcharts y-xis label override
- Why are JS / React much quicker than WebAssembly / Rust?
- React with VScode Editor
- How to refresh the facebook like url in reactjs (using facebook-sdk CDN)
- How to change state in react typescript
- How to move yellow part with text down to the white, when enough height is available?
- Toggling CSS as a state for li items in React
- React: How to update an object in an array from a child component
- How Should I Dispatch Actions When Using React Hooks?
- How to disable dropdown when a specific option is selected in React?
- webpack-dev-server not recompiling (JS files AND SCSS files)
- How to create new div after clicking a button in React,Js?
- Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- Customize multiple mui components
- How to allow for webpack-dev-server to allow entry points from react-router
- Accessing state value from StyleSheet.create in ReactNative
- Append react component
- React Native: Android is not showing all images (even though they are the same)
- Dependency is not renamed
- How to change the word "WEEKLY" to "Weekly" in react js
- Declare two types in interface Attributes