0

I am new to React JS and am just picking it up. Here is my code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>React - Component</title>
<script src="../../js/react.min.js"></script>
<script src="../../js/react-dom.min.js"></script>
<script src="../../js/browser.min.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
  var Comment= React.createClass({
    edit:function(){
        alert('Editing comment');
      },
    remove:function(){
        alert('Remove comment');
      },
    render:function(){
      return(
        <div classname="commentContainer">
         <div classname="commentText">Whatever</div>
            <button classname="button-primary" onclick={this.edit}>Edit</button>
            <button classname="button-danger" onclick={this.remove}>Delete</button>
        </div>
      );
    }
  });

  ReactDOM.render(
  <div classname="board">
    <Comment>LOL</Comment>
  </div>,
   document.getElementById('container')
  );
</script>

When I click the edit/ delete buttons in the browser they do not show the alert dialog. What am I doing wrong in the code?

3
  • Perhaps it should be onClick={this.edit.bind(this)}? You getting any errors? Commented Jan 24, 2017 at 17:16
  • onclick ==> onClick Commented Jan 24, 2017 at 17:16
  • I think you have the wrong casing for onClick, you need to have a capital C like onClick={this.edit}. Commented Jan 24, 2017 at 17:17

2 Answers 2

2

React event handlers need to be in camelCase: onClick (Notice the capital 'C')

Sign up to request clarification or add additional context in comments.

1 Comment

Works fine now thnx
0

You should use onClick instead onclick. See the working code below.

Read more about handling events in react here

  var Comment= React.createClass({
    edit:function(){
        alert('Editing comment');
      },
    remove:function(){
        alert('Remove comment');
      },
    render:function(){
      return(
        <div classname="commentContainer">
         <div classname="commentText">Whatever</div>
            <button classname="button-primary" onClick={this.edit}>Edit</button>
            <button classname="button-danger" onClick={this.remove}>Delete</button>
        </div>
      );
    }
  });

  ReactDOM.render(
  <div classname="board">
    <Comment>LOL</Comment>
  </div>,
   document.getElementById('container')
  );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>React - Component</title>
</head>
<body>
<div id="container"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.