Following the advice under Thinking In React…
I’ve built out my UI first, and just starting to add the functionality. I’ve got the handleClick
to hardcode changing the state. 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);
}
// static propTypes = {
// quote: PropTypes.string.isRequired,
// };
// handleClick(id) { this.setState(state => ({ display: {id} })); }
handleClick() { this.setState(state => ({ display: 7 })); }
render() {
return (
<Container id="calculator">
<Row className="justify-content-center">
<Col as={Button}
className="key-pad" xs={2} sm={2} md={1} lg={1}
id="seven"
value="7"
onClick={this.handleClick}
// onClick={this.handleClick.bind(this, id)}
// onClick={this.handleClick.bind(this, value)}
variant="success"
>
<h2>
7
</h2>
</Col>
</Row>
</Container>
);
}
}
export default Calculator;