React — onClick in component instance?

Kirk Ross

I have a <button> element inside a <BtnComponent /> (which is inside another component) and I'm trying to handle the click like this:

The BtnContainer.js parent component:

import React from 'react';
import Btn from './Btn';


var BtnContainer = React.createClass({

  handleClick() {
    alert('hello');
  },
  render() {
    return (
      <div id="container">
        <Btn onClick={this.handleClick} />
      </div>
    )
  }
});

module.exports = BtnContainer;

Here is the Btn.js component.

import React from 'react';

const Btn = () => (
  <button className="btn">Click</button>
);

export default Btn;

I tried placing the onClick in the actual button element too but got an error.

Thoughts?

Nader Dabit

Try this:

BtnContainer.js

import React from 'react';
import Btn from './Btn';


var BtnContainer = React.createClass({

  handleClick() {
    alert('hello');
  },
  render() {
    return (
      <div id="container">
        <Btn onClick={this.handleClick} />
      </div>
    )
  }
});

module.exports = BtnContainer;

Btn.js

import React from 'react';

const Btn = ({ onClick }) => (
  <button onClick={onClick} className="btn">Click</button>
);

export default Btn;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React onClick event on component

From Dev

Handling an onClick on a component in React

From Dev

Rendering a Component Instance in React

From Java

Append New React Component onclick

From Dev

React - return component onClick event

From Java

How to use onClick event on react Link component?

From Dev

React component how to simplify this onClick function?

From Java

onClick works but onDoubleClick is ignored on React component

From Dev

Meteor React component onClick event not working in IE

From Dev

onClick event isn't firing on React Component

From Dev

Testing onClick event on react component using Jasmine

From Dev

Call React Component Function from onclick in dangerouslySetInnerHTML

From Dev

onClick event isn't firing on React Component

From Dev

Access a parent component's method onClick in React

From Dev

React Component's onClick handler not binding to "this"

From Dev

"Cannot read property 'onClick' of undefined" in React component

From Dev

React--Button onclick event not working in Component

From Dev

React nested component wrong props function onClick

From Dev

how to access methods of an instance of a React component?

From Dev

is there any way to access the parent component instance in React?

From Dev

How to mock an instance created in React JS component?

From Dev

access react component's own instance in componentDidMount

From Dev

bind react instance method in class component

From Dev

inject content from one react component to another onClick

From Dev

Why does `this` keyword refer to Window not Component in React onClick() method

From Dev

React component only changes state on second onClick event

From Dev

onClick doesn't render new react component.

From Dev

React onClick inside .map then change data in another sibling component

From Dev

how should I build onClick action in my react component + Redux

Related Related

  1. 1

    React onClick event on component

  2. 2

    Handling an onClick on a component in React

  3. 3

    Rendering a Component Instance in React

  4. 4

    Append New React Component onclick

  5. 5

    React - return component onClick event

  6. 6

    How to use onClick event on react Link component?

  7. 7

    React component how to simplify this onClick function?

  8. 8

    onClick works but onDoubleClick is ignored on React component

  9. 9

    Meteor React component onClick event not working in IE

  10. 10

    onClick event isn't firing on React Component

  11. 11

    Testing onClick event on react component using Jasmine

  12. 12

    Call React Component Function from onclick in dangerouslySetInnerHTML

  13. 13

    onClick event isn't firing on React Component

  14. 14

    Access a parent component's method onClick in React

  15. 15

    React Component's onClick handler not binding to "this"

  16. 16

    "Cannot read property 'onClick' of undefined" in React component

  17. 17

    React--Button onclick event not working in Component

  18. 18

    React nested component wrong props function onClick

  19. 19

    how to access methods of an instance of a React component?

  20. 20

    is there any way to access the parent component instance in React?

  21. 21

    How to mock an instance created in React JS component?

  22. 22

    access react component's own instance in componentDidMount

  23. 23

    bind react instance method in class component

  24. 24

    inject content from one react component to another onClick

  25. 25

    Why does `this` keyword refer to Window not Component in React onClick() method

  26. 26

    React component only changes state on second onClick event

  27. 27

    onClick doesn't render new react component.

  28. 28

    React onClick inside .map then change data in another sibling component

  29. 29

    how should I build onClick action in my react component + Redux

HotTag

Archive