Hello,
I’m trying to rebuild the caculator with Reacat
Here is a part of my code
from App component I’m passing handleValue() to the Childe component Btns
And then when I click the button in Btns I want to update App state value
How correctly update App state value?
App component
class App extends React.Component {
state = {
value: null
};
handleValue(e) {
e.preventDefault();
this.setState({
value: e.target.name
});
}
render() {
return (
<div className="container">
<div className="calculator">
<Screen value={this.state.value} />
<Btns value={this.handleValue} />
</div>
</div>
);
}
}
export default App;
Btns component
class Btns extends React.Component {
handleClick = e => {
e.preventDefault();
this.props.value = e.target.name;
};
render() {
return (
<div className="keys">
<button name="ac" onClick={this.handleClick} id="ac">
AC
</button>
<button name="/" onClick={this.handleClick} id="divide">
/
</button>
<button name="*" onClick={this.handleClick} id="multipl">
*
</button>
<button name="7" onClick={this.handleClick} id="seven">
7
</button>
<button name="8" onClick={this.handleClick} id="eight">
8
</button>
<button name="9" onClick={this.handleClick} id="nine">
9
</button>
<button name="-" onClick={this.handleClick} id="minus">
-
</button>
<button name="4" onClick={this.handleClick} id="four">
4
</button>
<button name="5" onClick={this.handleClick} id="five">
5
</button>
<button name="6" onClick={this.handleClick} id="six">
6
</button>
<button name="+" onClick={this.handleClick} id="plus">
+
</button>
<button name="1" onClick={this.handleClick} id="one">
1
</button>
<button name="2" onClick={this.handleClick} id="two">
2
</button>
<button name="3" onClick={this.handleClick} id="three">
3
</button>
<button name="=" onClick={this.handleClick} id="equal">
=
</button>
<button name="0" onClick={this.handleClick} id="zero">
0
</button>
<button name="." onClick={this.handleClick} id="dot">
.
</button>
</div>
);
}
}
export default Btns;