Click here to Skip to main content
15,885,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to create a calculator in React, with an output field updating once user inputs one of the values. It has to be within a class.

JavaScript
class CreateProjectAlter extends React.Component {

render() {
    const { classes } = this.props;

    //EBIT
    const initialebitTotalSpend = Number(localStorage.getItem("ebitTotalSpend") || 0);
    const initialebitTotalSpendNewContract = Number(localStorage.getItem("ebitTotalSpendNewContract") || 0);
    const initialebitImpactValue = Number(localStorage.getItem("ebitImpactValue") || 0);
    const initialebitImpactPercentage = Number(localStorage.getItem("ebitImpactPercentage") || 0);
    const initialebitdaDKK = Number(localStorage.getItem("ebitdaDKK") || 0);


     const [ebitTotalSpend, setebitTotalSpend] = useState(initialebitTotalSpend)
     const [ebitTotalSpendNewContract, setebitTotalSpendNewContract] = useState(initialebitTotalSpendNewContract)
     const [ebitImpactValue, setebitImpactValue] = useState(initialebitImpactValue)
    const [ebitImpactPercentage, setebitImpactPercentage] = useState(initialebitImpactPercentage)
    const [ebitdaDKK, setebitdaDKK] = useState(initialebitdaDKK)

    useEffect (() => {
      localStorage.setItem("ebitTotalSpend", ebitTotalSpend)
      localStorage.setItem("ebitTotalSpendNewContract", ebitTotalSpendNewContract)
      localStorage.setItem("ebitImpactValue", ebitImpactValue)
      localStorage.setItem("ebitImpactPercentage", ebitImpactPercentage)
      localStorage.setItem("ebitdaDKK", ebitdaDKK)
    }, [
      ebitTotalSpend, ebitTotalSpendNewContract
    ])  

    let calcebitplusovidkk = (ebitTotalSpend-ebitTotalSpendNewContract)

    return (
      <div>
      
       <Grid item xs={12} md={4} lg={4}>
              <Card>
                <Box p={3}>
                  <CardHeader
                    title="EBIT"
                    align="center"
                    style={{ backgroundColor: "#002B45", color: "white" }}
                  />
                  <TextField
                    id="ebitTotalSpend"
                    value={ebitTotalSpend}
                    label="Total spend / Baseline (DKK)"
                    variant="outlined"
                    fullWidth
                    margin="normal"
                  />
                  <TextField
                    id="ebitTotalSpendNewContract"
                    value={ebitTotalSpendNewContract}
                    label="Total spend new contract (DKK)"
                    variant="outlined"
                    fullWidth
                    margin="normal"
                  />
                  <TextField
                    id="ebitImpactValue"
                    // value={ebitImpactValue}
                    value={calcebitplusovidkk}          
                    label="Impact (DKK)"
                    variant="outlined"
                    fullWidth
                    margin="normal"
                  />
                  <TextField
                    id="ebitImpactPercentage"
                    value={ebitImpactPercentage}
                    onChange={(e) => setebitImpactPercentage(parseInt(e.target.value) || 0)} 
                    label="Impact (%)"
                    variant="outlined"
                    fullWidth
                    margin="normal"
                  />
                  <TextField
                    id="ebitdaDKK"
                    value={ebitdaDKK}
                    label="Ebitda (DKK)"
                    variant="outlined"
                    fullWidth
                    margin="normal"
                    onChange={(e) => setebitdaDKK(parseInt(e.target.value) || 0)} 

                  />
                </Box>
              </Card>
            </Grid>
            <Grid item xs={12} md={4} lg={4}>
              <Card>
                <Box p={3}>
                  <CardHeader
                    title="EBIT+"
                    align="center"
                    style={{ backgroundColor: "#002B45", color: "white" }}
                  />
                  <TextField
                    id="ebitPlusTotalSpend"
                    label="Total spend / Baseline (DKK)"
                    variant="outlined"
                    fullWidth
                    margin="normal"
                  />
                  <TextField
                    id="ebitPlusTotalSpendNewContract"
                    label="Total spend new contract (DKK)"
                    variant="outlined"
                    fullWidth
                    margin="normal"
                  />
                  <TextField
                    id="ebitPlusImpactValue"
                    label="Impact (DKK)"
                    variant="outlined"
                    fullWidth
                    margin="normal"
                  />
                  <TextField
                    id="ebitPlusImpactPercentage"
                    label="Impact (%)"
                    variant="outlined"
                    fullWidth
                    margin="normal"
                  />
                </Box>
              </Card>
            </Grid>
            
            </div>


What I have tried:

I have been trying to do it using hooks, but I am receiving a following error (invalid hook call).
Does anyone knows how should I proceed with it? Should it be done with hooks, or are there any alternatives?
Posted
Updated 27-Sep-21 12:04pm

the question is not clear , you wanna a calculator that shows an output updated once you update the input ? is that your question?
 
Share this answer
 
Comments
Member 15341738 27-Sep-21 17:42pm    
That is correct, yes. I am sorry if my question is hard to understand, but english is not my native language.
saiche nouh 27-Sep-21 18:04pm    
don't be sorry that's right , why are you using local storage to store the data in the browser ? take a look at that simple example for multiplying and you can get the concept for the others operations (code bellow)

JavaScript
import "./styles.css";
import {useState} from "react";
export default function App() {
  const [input1 , setInput1] = useState(1);
  const [input2 , setInput2] = useState(1);
  const [output , setOutput] = useState(1);


  return (
    <div className="App">
      <input value={input1} onChange={(event)=>{
        setInput1(event.target.value)
        setOutput(event.target.value * input2)
      }}/>
    <input value={input2} onChange={(event)=>{
        setInput2(event.target.value)
        setOutput(input1 * event.target.value)
      }}/>      

<input value={output}/>
    </div>
  );
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900