-1

//I have an error message "Cannot read property 'value' of null", maybe there are some that can help?

import React from 'react'

function Calculator() {

    function Calculation() {

        const theValueOne = parseInt(document.querySelector("#valueOne").value);
        const theValueTwo = parseInt(document.querySelector("#valueTwo").value);
        const theOperator = document.querySelector("#operators").value;
        let theCalculation;

        if (theOperator === "addition") {
            theCalculation = theValueOne + theValueTwo;
        } else if (theOperator === "minus") {
            theCalculation = theValueOne - theValueTwo;
        } else if (theOperator === "multiply") {
            theCalculation = theValueOne * theValueTwo;
        } else if (theOperator === "divide") {
            theCalculation = theValueOne / theValueTwo;
        }

        document.querySelector("#resualt");
    }
    return (
        <div>
            <form>
                Value 1: <input type="text" id="valueOne" />
                Value 2: <input type="text" id="valueTwo" />
                Operator:
                <select id="operators">
                    <option value="addition"></option>
                    <option value="minus"></option>
                    <option value="multiply"></option>
                    <option value="divide"></option>
                </select>
                <button type="button" onClick={Calculation()}>Calculate</button>
            </form>
            <div id="resualt"></div>
        </div>
    )
}

export default Calculator
justDan
  • 2,049
  • 5
  • 14
  • 27
  • The element does not exist until it gets rendered, so you can't access the element's value at the beginning of the component. Retrieve it only when you need to, after render, when there's a `theOperator`. Even better, use React handlers instead of vanilla DOM handlers – CertainPerformance Dec 01 '20 at 17:45

1 Answers1

0

You can use React Hooks to get the value of your inputs and of the operator everywhere in the functional component

import React, { useState } from "react";

function Calculator() {
  const [valueOne, setValueOne] = useState();
  const [valueTwo, setValueTwo] = useState();
  const [operator, setOperator] = useState();
  const [result, setResult] = useState();

  function Calculation() {
  // Here you can access valueOne, valueTwo, operator directly
  let theCalculation;

  ...

  setResult(theCalculation);

Then you can directly get the value of your inputs by adding value and onChange props to your input tags

<form>
Value 1: <input type="number" id="valueOne" value={valueOne} onChange={(e) => setValueOne(e.target.value) />
Value 2: <input type="number" id="valueTwo" value={valueTwo} onChange={(e) => setValueTwo(e.target.value) />
Operator:
<select id="operators" value={operator} onChange={(e) => setOperator(e.target.value)}>
  <option value="addition"></option>
  <option value="minus"></option>
  <option value="multiply"></option>
  <option value="divide"></option>
</select>
<button type="button" onClick={calculation}>Calculate</button>
<div id="resualt">{result}</div>
</form>
VersifiXion
  • 1,492
  • 5
  • 23