React - onClick events

0 votes
787 views
added Mar 8, 2019 in React by LC Marshal Captain (25,790 points)
edited May 17, 2019 by LC Marshal
//React - onClick and alert

class Square extends React.Component {
  render() {
    return (
​     //using arrow function syntax
      <button className="square" onClick={() => alert('click')}>
      //equivelant to
      //<button className="square" onClick={function() {alert ('i get clicked'); }}>
        {this.props.value}
      </button>
    );
  }
}

 

8 Responses

0 votes
responded May 17, 2019 by LC Marshal Captain (25,790 points)
class TransactionCategoryResultsComponent extends React.Component {

constructor(props) {
  super(props);
  this.state = {
    value: 0,
    message: 'default click state',
};

onClick = () => {
  this.setState({
    value: this.state.value + 1
  });
  
  this.setState({
    message: `click-state ${this.state.value}`
  });
}

render() {
return (
<div className="testtest">
  <div>render->state={this.state.value} - 
    {this.state.message}
  </div>
  <button onClick={this.onClick}>Click </button>
</div>

 

0 votes
responded May 17, 2019 by LC Marshal Captain (25,790 points)
// Listening to events

class DisplayAlert extends Component {
  DisplayAlert() {
    alert("Alert here!");
  }

  render() {
    return <button onClick={this.DisplayAlert}>show alert</button>;
  }
}

export default DisplayAlert;

const rootElement = document.getElementById("root");
ReactDOM.render(<ShowAlert />, rootElement);

 

0 votes
responded May 17, 2019 by LC Marshal Captain (25,790 points)
// use class fields to correctly bind callbacks

class LoggingButton extends React.Component {
  // This syntax ensures `this` is bound within handleClick.
  // Warning: this is *experimental* syntax.
  handleClick = () => {
    console.log('this is:', this);
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        Click me
      </button>
    );
  }
  
  // ref. from: https://reactjs.org/docs/handling-events.html

 

0 votes
responded May 17, 2019 by LC Marshal Captain (25,790 points)
// Binding in render()

import React, { Component } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

class ChangeInput extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: ""
    };
  }

  changeText(event) {
    this.setState({
      name: event.target.value
    });
  }

  render() {
    return (
      <div>
        <label htmlFor="name">Enter Text here </label>
        <input type="text" id="name" onChange={this.changeText.bind(this)} />
        <h3>{this.state.name}</h3>
      </div>
    );
  }
}

export default ChangeInput;

const rootElement = document.getElementById("root");
ReactDOM.render(<ChangeInput />, rootElement);

// ref: https://blog.logrocket.com/a-guide-to-react-onclick-event-handlers-d411943b14dd

 

0 votes
responded May 17, 2019 by LC Marshal Captain (25,790 points)
// Binding in constructor()

import React, { Component } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

class ChangeInput extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: ""
    };

    this.changeText = this.changeText.bind(this);
  }

  changeText(event) {
    this.setState({
      name: event.target.value
    });
  }

  render() {
    return (
      <div>
        <label htmlFor="name">Enter Text here </label>
        <input type="text" id="name" onChange={this.changeText} />
        <h3>{this.state.name}</h3>
      </div>
    );
  }
}

export default ChangeInput;

const rootElement = document.getElementById("root");
ReactDOM.render(<ChangeInput />, rootElement);

 

0 votes
responded May 17, 2019 by LC Marshal Captain (25,790 points)
// Binding with arrow function

import React, { Component } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

class ChangeInput extends Component {
  handleEvent = event => {
    alert("I was clicked");
  };

  render() {
    return (
      <div>
        <button onClick={this.handleEvent}>Click on me</button>
      </div>
    );
  }
}

export default ChangeInput;

const rootElement = document.getElementById("root");
ReactDOM.render(<ChangeInput />, rootElement);

// ref: https://blog.logrocket.com/a-guide-to-react-onclick-event-handlers-d411943b14dd

 

0 votes
responded May 17, 2019 by LC Marshal Captain (25,790 points)
const TwitterShare = () => {
  window.open('http://twitter.com/share?url='+window.location.href,+'targetWindow','height=600,width=660,resizable=no,toolbar=no,menubar=no,status=no,location=no,scrollbars=yes');
  };

<a target='_blank' onClick={TwitterShare}><img className="mb-3" src={TwitterIcon} alt="property" /></a>

 

0 votes
responded Apr 14, 2021 by lcjr First Warrant Officer (11,530 points)
<button
  className="btn secondary-grey enquire-button"
  type="button"
  onClick={e => openModal(e, false)}
>
  {enquireBtnTxt}
</button>

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...