Tell us what’s happening:
setDisplay() method should take a click event and set state accordingly.
I am receiving following error message:
" Objects are not valid as a React child (found: object with keys {count, tempcount}).
If you meant to render a collection of children, use an array instead."
It appears that merhod is taking component state as parameter, instead of the value that is taken from the click event.
This does not make much sence to me and I do not have debugging skills to investigate further.
May I kindly ask someone for help.
Many thanks in advance.
P.S.
I should add that I am trying my best in solving problems, but. more often than not apparently, I am puzzled with what to do.
I do hope my asking for help will decrease as I get more experienced.
Your code so far
const digits = [
["zero", 0],
["one", 1],
["two", 2],
["three", 3],
["four", 4],
["five", 5],
["six", 6],
["seven", 7],
["eight", 8],
["nine", 9]
];
const operators = [
["add", "+"],
["subtract", "-"],
["multiply", "*"],
["divide", "/"],
["decimal", "."],
["equals", "="],
["clear", "AC"]
];
class Calculator extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
tempcount: 0
};
this.setDisplay = this.setDisplay.bind(this);
}
// take value from the event listener and set state.tempcount to this value.
//tempcount should then be rendered to the screen within div with id='display'
setDisplay() {
this.setState(e=>({
tempcount:e
}));
}
render() {
// console.log(this.state.tempcount)
//render numbers to the screen
const padDigits = digits.map((arr, num, dig) => {
//console.log(arr[1]);
return (
<button id={arr[0]} className="buttons" onClick={this.setDisplay} value={arr[1]}>
{num}
</button>
);
});
//render operators to the screen
const padOperators = operators.map((arr, oper, arrArr) => {
return (
<button id={arr[0]} className="buttons">
{arr[1]}
</button>
);
});
return (
<div id="container">
<div id="display">{this.state.tempcount}</div>
<div id="digits">{padDigits}</div>
<div id="operators">{padOperators}</div>
</div>
);
}
}
ReactDOM.render(<Calculator />, document.getElementById("App"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36
Challenge: Build a JavaScript Calculator
Link to the challenge: