In this recipe, we are going to learn how to simulate the onClick and onChange events on a simple Calculator component.
Simulating Events
How to do it...
We will re-use the code of the last recipe (Repository: Chapter12/Recipe3/debugging):
- We will create a simple Calculator component to sum two values (input) and then we will get the result when the user clicks on the equals (=) button:
import React, { Component } from 'react';
import styles from './Calculator.scss';
class Calculator extends Component {
state = {
number1: 0,
number2: 0,
result: 0
};
handleOnChange = e => {
const { target: { value, name } } = e;
this.setState({
[name]: value
});
...