lun7code
lun7code

Reputation: 199

react button no response or display any message when clicked

I try to click button to test the it was clicked or not? But I found it no response when i click it, is that I missing something?

import React, { Component } from 'react';
import './App.css';

class App extends Component {

  switchNameHandler = () =>{
    console.log('was clicked');
  };

  render() {
    return (
      <div className="App">
        <h1>Hi, I'm a React App</h1>
          <h1>Another heading</h1>
          <button onClick={this.switchNameHandler()}>Switch Names</button>
      </div>
    );
  }
}

export default App;

Upvotes: 0

Views: 509

Answers (1)

Ariel Salem
Ariel Salem

Reputation: 357

try changing your button to:

<button onClick={() => this.switchNameHandler()}>Switch Names</button> 

this should invoke the call when the button is clicked

Upvotes: 1

Related Questions