const Calculatorpage = () => {
function calculate() {
const firstNum = Number(document.getElementById('firstNum').value)
const secondNum = Number(document.getElementById('secondNum').value)
const symbol = document.getElementById('symbol').value
const result = document.getElementById('result')
console.log("im in");
let res
switch (symbol) {
case "+": res = secondNumber + firstNumber
break;
default:
break;
}
result.value = res
}
return (
<div>
<input id="firstNum" type="number" />
<input id="symbol" placeholder="+" type="symbol" />
<input id="secondNum" type="number" />
<input id="result" type="number" />
<button type="submit" onClick={calculate}>Go</button>
</div>
)
}
This is how I am using it, but it says that case is a forbidden word?
how can use switch case and what’s the syntax I am supposed to use, I don’t want to use if else constructor?
3
Answers
Please check this , your code is correct just need to cleaned up :
demo on codesandbox
Can you attempt this which I think is a way cleaner too: Get that switch out of the render in a function and just call it passing the params you want
Using native DOM methods with React is not "the React Way". It will cause conflicts as React has its own way of reconciling changes to the DOM through state manipulation.
So here’s an example that uses state.