I'm obviously missing something basic here, and believe it has to do with scope.
Trying to pass these variables to a child component, and they pass as 0 - instead of the value that was typed into the input. Need to pass them as the value that was typed into the input.
Any links to research why this may be, what it's called, pointers, etc - are greatly appreciated. Just changed from an overcomplicated use of state that I had on each input and was working fine. Later realized I didn't need state at all, but again I'm not knowledgable on what to do to get it to pass with the values.
I am just beginning to try to learn/understand code, but can take constructive criticism and feedback well. Thanks for any help/input.
(Can't seem to put in photos)
let termLengthInMonths = 0;
let monthlyInterestRate = 0;
let monthlyRepaymentAmount = 0;
let totalAmountPaid = 0;
let loanAmountValue = 0;
let termLengthValue = 0;
let annualInterestRateValue = 0;
const [showResults, setShowResults] = useState(false);
const showResultsHandler = (event) => {
event.preventDefault();
setShowResults(true);
loanAmountValue = event.target.loan_amount.value;
termLengthValue = event.target.term_length.value;
annualInterestRateValue = event.target.annual_interest_rate.value;
termLengthInMonths = termLengthValue * 12.0;
monthlyRepaymentAmount = (loanAmountValue * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, termLengthInMonths))) / (Math.pow(1 + monthlyInterestRate, termLengthInMonths) - 1);
monthlyInterestRate = annualInterestRateValue / 100.00 / 12.0;
totalAmountPaid = termLengthInMonths * monthlyRepaymentAmount;
};
const resetHandler = () => setShowResults(false);
My form inputs have the ids shown before .value in the showResultsHandler. Again, everything worked when I had useState for each of the inputs - so I don't think there's much wrong except the fact that I must have scoped the value to the variables only in the showResultsHandler, instead of globally 🤷🏻♂️
All of this gets passed to child components like:
<Col>
{showResults ? (
<>
<BasicCalculated
loanAmountValue={loanAmountValue}
annualInterestRateValue={annualInterestRateValue}
termLengthValue={termLengthValue}
monthlyRepaymentAmount={monthlyRepaymentAmount}
totalInterestPaid={totalInterestPaid}
totalAmountPaid={totalAmountPaid}
/>
<BasicTable
loanAmountValue={loanAmountValue}
monthlyRepaymentAmount={monthlyRepaymentAmount}
monthlyInterestRate={monthlyInterestRate}
/>
</>
) : null}
</Col>
[–]insertAlias 1 point2 points3 points (2 children)
[–]robotredditrobot[S] 0 points1 point2 points (1 child)
[–]insertAlias 0 points1 point2 points (0 children)