Now, it's time for you to check back on everything! You will be building a calculator with React, and only minimal instructions have been provided for you to really think about what's happening.
At first, your calculator will just add 2 numbers together when they are typed in. For the bonus, you might decide to get more creative.
Fork & Clone this repo, cd into repo, run npx create-react-app .
to turn your cloned repo into a react app.
Start by creating a single component file in the src
directory, and name it Calculator.js
. In this file, create your Calculator
class. Use the App.js
as an example of how to create a basic component. Add the following JSX to your Calculator's render()
function:
<div className="container">
<h1>Add with React!</h1>
<div className="add">
<input type="text" />
<span>+</span>
<input type="text" />
<button>=</button>
<h3>Addition results go here!</h3>
</div>
</div>
Set up the initial state of your component. What state attributes will you need to track? What values should those state items start with? Where is that state displayed in the browser?
Hint: You will only need one state. That one state can hold as many key-value pairs as you need!
You will want to trigger a function when the values in your textboxes change. You can capture these values by setting a function on the onChange property. Let's say I have a textbox tracking my first number.
<input type="number"
name="num1"
placeholder="Enter your first number"
value={this.state.num1}
onChange={ (e) => this.setNum(e, 'num1') }
/>
I want to store this number as part of my state. Let's say I decided to call it num1
. I could set my state like so:
setNum = (e, num) => {
this.setState({ [num]: e.target.value});
}
Hint: The [] are there so we can use a dynamic key value! This value becomes
num1
ornum2
depending on what was clicked and sent to the function fromonChange
.
The above method relies on a closure to determine which button is being clicked, which is a common technique in React. Can you think of another way(s) to accomplish this task without using a closure?
If you decided to use buttons for your calculator, you probably want to use onClick
instead of onChange
, but the concepts are the same! Here is some documentation to help you choose what you want to do and how to do it:
Once you've got your event handlers set up to capture the input, you'll need to create a method for your submit button. The method should accept the triggered event, get the input values from your state, add them together, and set part of the state to the new sum
.
Hint: Where should this method go?
In the same component as it's being used - between the constructor and the render.
Thought: How will you handle inputs that aren't numbers?
Once the state of the sum
has been set, React will re-render the whole component. Make sure you have a place in your JSX that displays the result!
-
Make the calculator work with any of the 4 basic arithmetic operations
(+, -, *, /). How will this change your
state
and your JSX?
- All content is licensed under a CC-BY-NC-SA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.