I create a simple calculator in React. But I am confusing because I don't know how to implement 'calculate function' without eval(). I have read the eval() function is harmful.
This is my code:
function App() {
const [numbers, setNumbers] = useState('')
const alert = 'Choose the numbers!'
// Function responsible for add numbers and symbols to the useState
const handleClickAddNumber = e => {
const addNumbers = numbers.concat(e.target.value)
setNumbers(addNumbers)
}
// Clear the function
const handleClickClear = () => {
setNumbers('')
}
// Back to the previous state
const handleClickBack = () => {
const back = numbers.slice(0, numbers.length -1)
setNumbers(back)
}
// The function responsible for the calculate
const handleClickCalculate = () => {
try {
const calculate = eval(numbers).toString()
setNumbers(calculate)
} catch (error) {
console.log('Error')
}
}
What can I implement instead of eval()?