Я пересмотрел код после небольшого перерыва, и да, главная проблема заключалась в том, что значения a, b и c действительно были строками, а не целыми числами, отсюда и ошибка в значении области. Справочный артикул: https://flaviocopes.com/how-to-convert-string-to-number-javascript/
Обновлен код ниже:
import React from 'react'
import Area from './Area'
import classes from './App.module.css'
const doArea = (a, b, c) => {
const s = (a + b + c) / 2;
return Math.sqrt(s * ((s - a) * (s - b) * (s - c)));
//return Math.sqrt(s * ((s - a) * (s - b) * (s - c))).toFixed(2);
}
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
sides: [
{ text: 'Introduce the length for side a', value: undefined, id: 0 },
{ text: 'Introduce the length for side b', value: undefined, id: 1 },
{ text: 'Introduce the length for side c', value: undefined, id: 2 }
],
area: undefined
}
}
onLengthChange = ({ id, value }) => {
console.log(value)
console.log(typeof value)
const sides = this.state.sides;
const _sides = sides.map(side => {
if (side.id === id) {
return {
...side,
value: value,
}
} else {
return side
}
});
this.setState({ sides: _sides })
}
onCalculateArea = () => {
console.log(typeof this.state.area)
const sides = this.state.sides;
const sideA = parseInt(sides[0].value) || 1,
sideB = parseInt(sides[1].value) || 1,
sideC = parseInt(sides[2].value) || 1;
this.setState({ area: doArea(sideA, sideB, sideC) });
}
render() {
const props = this.state;
const inputList = props.sides.map((side, index) => {
return <Area
inputText={side.text}
key={`Input-` + index}
inputId={side.id}
onInputChange={
(event) =>
this.onLengthChange({
id: side.id,
value: event.target.value
})}
/>
})
return (
<div className={classes.background}>
...
</div>
)
}
}
export default App;