Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor class based component to a functional component with hooks #4

Merged
merged 2 commits into from Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 39 additions & 48 deletions src/components/calculator.js
@@ -1,57 +1,48 @@
import React from 'react';
import React, { useState } from 'react';
import './calculator.css';
import calculate from '../logic/calculate';

class Calculator extends React.Component {
constructor(props) {
super(props);
const Calculator = () => {
const [state, setState] = useState({
total: 0,
next: null,
operation: null,
});

this.state = {
total: 0,
next: null,
operation: null,
};

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

handleEvent = (buttonName) => {
this.setState((state) => calculate(state, buttonName));
const handleEvent = (e) => {
setState({ ...state, ...calculate(state, e.target.name) });
};
Comment on lines +12 to 14
Copy link

@henatan99 henatan99 Sep 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • When an operator button is clicked multiple times consecutively, it throws an error on the browser. Even though you did a great job refactoring the Calculator component to a functional component, it is required that your app is free of error. Kindly add an error handling condition or method. 🙏

Screenshot from 2021-09-23 20-48-25

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing my project! I didn't do an error handling condition or method because I found the error. The function when the user presses an operation without having entered a number first return nothing was commented.


render() {
const { total, operation, next } = this.state;
return (
<div>
<p className="result">
<span>{total}</span>
<span>{operation}</span>
<span>{next}</span>
</p>
<div className="calculator">
<button type="button" className="grayButton" name="AC" onClick={(e) => this.handleEvent(e.target.name)}>AC</button>
<button type="button" className="grayButton" name="+/-" onClick={(e) => this.handleEvent(e.target.name)}>+/-</button>
<button type="button" className="grayButton" name="%" onClick={(e) => this.handleEvent(e.target.name)}>% </button>
<button type="button" className="orangeButton" name="÷" onClick={(e) => this.handleEvent(e.target.name)}>÷</button>
<button type="button" className="grayButton" name="7" onClick={(e) => this.handleEvent(e.target.name)}>7</button>
<button type="button" className="grayButton" name="8" onClick={(e) => this.handleEvent(e.target.name)}>8</button>
<button type="button" className="grayButton" name="9" onClick={(e) => this.handleEvent(e.target.name)}>9</button>
<button type="button" className="orangeButton" name="x" onClick={(e) => this.handleEvent(e.target.name)}>x</button>
<button type="button" className="grayButton" name="4" onClick={(e) => this.handleEvent(e.target.name)}>4</button>
<button type="button" className="grayButton" name="5" onClick={(e) => this.handleEvent(e.target.name)}>5</button>
<button type="button" className="grayButton" name="6" onClick={(e) => this.handleEvent(e.target.name)}>6</button>
<button type="button" className="orangeButton" name="-" onClick={(e) => this.handleEvent(e.target.name)}>-</button>
<button type="button" className="grayButton" name="1" onClick={(e) => this.handleEvent(e.target.name)}>1</button>
<button type="button" className="grayButton" name="2" onClick={(e) => this.handleEvent(e.target.name)}>2</button>
<button type="button" className="grayButton" name="3" onClick={(e) => this.handleEvent(e.target.name)}>3</button>
<button type="button" className="orangeButton" name="+" onClick={(e) => this.handleEvent(e.target.name)}>+</button>
<button type="button" className="grayButton-0" name="0" onClick={(e) => this.handleEvent(e.target.name)}>0</button>
<button type="button" className="grayButton" name="." onClick={(e) => this.handleEvent(e.target.name)}>.</button>
<button type="button" className="orangeButton" name="=" onClick={(e) => this.handleEvent(e.target.name)}>=</button>
</div>
return (
<div>
<p className="result">
<span>{state.total}</span>
<span>{state.operation}</span>
<span>{state.next}</span>
</p>
<div className="calculator">
<button type="button" className="grayButton" name="AC" onClick={handleEvent}>AC</button>
<button type="button" className="grayButton" name="+/-" onClick={handleEvent}>+/-</button>
<button type="button" className="grayButton" name="%" onClick={handleEvent}>% </button>
<button type="button" className="orangeButton" name="÷" onClick={handleEvent}>÷</button>
<button type="button" className="grayButton" name="7" onClick={handleEvent}>7</button>
<button type="button" className="grayButton" name="8" onClick={handleEvent}>8</button>
<button type="button" className="grayButton" name="9" onClick={handleEvent}>9</button>
<button type="button" className="orangeButton" name="x" onClick={handleEvent}>x</button>
<button type="button" className="grayButton" name="4" onClick={handleEvent}>4</button>
<button type="button" className="grayButton" name="5" onClick={handleEvent}>5</button>
<button type="button" className="grayButton" name="6" onClick={handleEvent}>6</button>
<button type="button" className="orangeButton" name="-" onClick={handleEvent}>-</button>
<button type="button" className="grayButton" name="1" onClick={handleEvent}>1</button>
<button type="button" className="grayButton" name="2" onClick={handleEvent}>2</button>
<button type="button" className="grayButton" name="3" onClick={handleEvent}>3</button>
<button type="button" className="orangeButton" name="+" onClick={handleEvent}>+</button>
<button type="button" className="grayButton-0" name="0" onClick={handleEvent}>0</button>
<button type="button" className="grayButton" name="." onClick={handleEvent}>.</button>
<button type="button" className="orangeButton" name="=" onClick={handleEvent}>=</button>
</div>
);
}
}
</div>
);
};

export default Calculator;
7 changes: 4 additions & 3 deletions src/logic/calculate.js
Expand Up @@ -91,11 +91,12 @@ export default function calculate(obj, buttonName) {

// When the user presses an operation button without having entered
// a number first, do nothing.
// if (!obj.next && !obj.total) {
// return {};
// }
if (!obj.next && !obj.total) {
return {};
}

// User pressed an operation after pressing '='

if (!obj.next && obj.total && !obj.operation) {
return { ...obj, operation: buttonName };
}
Expand Down