Following the advice under Thinking In React…
I’ve built out my UI first, with no real functionality to anything yet.
Now that you have your component hierarchy, it’s time to implement your app. The easiest way is to build a version that takes your data model and renders the UI but has no interactivity. It’s best to decouple these processes because building a static version requires a lot of typing and no thinking, and adding interactivity requires a lot of thinking and not a lot of typing. We’ll see why.
Starting to add functionality. I’ve got the handleClick
hardcoded changing the state to seven, no matter if I click on 7, 8 or 9 it puts 7 in the display. And clear sets the State of the Display back to zero. This was to check that the basics are working, the function is binding and firing as expected.
The problem is when I try to pass in which button called the event. If I console.log(e) it is still undefined. But now I want to pass in either the id
or the value
attribute
from each button. You can see what I’ve tried and it’s been commented out. Basically, it’s telling me the id or the value are undefined but am I not declaring those on the line right above it?
Passing 6 out of 16, for the Calculator at FreeCodeCamp.com
repo
live demo, be sure to use the NavBar to get to Calculator
use the hamburger menu on the top left and select JavaScript Calculator to run the test suite, they say it’s designed for Chrome and may encounter bugs in other browsers.
Calculator.js
import React, { Component } from 'react';
// import PropTypes from 'prop-types';
import { Button, Container, Row, Col } from 'react-bootstrap';
import './Calculator.css';
export class Calculator extends Component {
constructor(props) {
super(props);
// Initial State
this.state = {
display: 0,
};
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
this.handleClear = this.handleClear.bind(this);
}
// static propTypes = {
// quote: PropTypes.string.isRequired,
// };
// handleClick(id) { this.setState(state => ({ display: {id} })); }
handleClick(e) {
console.log({ e });
this.setState(state => ({ display: 7 }));
// this.setState(state => ({ display: {e} }));
}
![Screenshot from 2021-01-03 10-30-56](https://user-images.githubusercontent.com/7327259/103482468-0d23d600-4daf-11eb-873a-832410c1e4c6.png)
handleClear() { this.setState(state => ({ display: 0 })); }
render() {
return (
<Container
id="calculator">
<Row className="justify-content-center">
<Col as={Button}
value="/"
id="divide">
<h2>
<i class="fas fa-divide"></i>
</h2>
</Col>
<Col as={Button}
id="seven"
value="7"
// onClick={this.handleClick}
onClick={(e) => this.handleClick(this.id, e)}
// onClick={this.handleClick.bind(this, id)}
// onClick={e => this.handleClick(this.value)}
// onClick={this.handleClick.bind(this, id)}
// onClick={this.handleClick.bind(this, this.props.value)}
>
<h2>
7
</h2>
</Col>
<Col as={Button}
value="8"
onClick={this.handleClick}
id="eight">
<h2>
8
</h2>
</Col>
<Col as={Button}
value="9"
onClick={this.handleClick}
id="nine">
<h2>
9
</h2>
</Col>
</Row>
</Container>
);
}
}
export default Calculator;