1
class Application extends React.Component {
  render() {
    return [div]
      [button onclick={() => {console.log('click'}}]button[/button]
    [/div];
  }
}

React.render(<Application />, document.getElementById('app'));

I have the above code in my react app, render component. Nothing happens as nothing print in console.

4
  • Can you provide all of the code for the entire component? Commented Sep 15, 2019 at 3:43
  • <button onClick={() => {console.log('clicked')}}> button </button> Commented Sep 15, 2019 at 3:49
  • Are you using any prettier? < and > getting converted to [ and ]. Commented Sep 15, 2019 at 3:52
  • onclick is wrong . onClick is true Commented Sep 15, 2019 at 5:59

2 Answers 2

5

React uses camelCase. onclick should be onClick.

<button onClick={() =>{console.log('clicked')}}>button</button>
Sign up to request clarification or add additional context in comments.

Comments

0

It's just that you have made a syntax mistake here. React uses camelCase for event handlers.

For example, the HTML:

<button onclick="activateLasers()">
  Activate Lasers
</button>

is slightly different in React:

<button onClick={activateLasers}>
  Activate Lasers
</button>

So your code becomes:

class Application extends React.Component {
  render() {
    return ( 
      <div>
       <button onClick={() => {console.log('click'}}]button </button>
      </div>
    )
  }
}

React.render(<Application />, document.getElementById('app'));

Comments

Your Answer

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