0
I’m making an app on React which calculates compound interest and would like to know if anyone knows how to create several components at once dynamically.
Example: if I put 12 months I will have to generate 12 components of the type Box the amount, rate of increase and difference in the value of the initial amount.
I’d like to call the component Box several times creating the boxes that would actually be divs with the information.
index.js:
useEffect(() => {
var diference = 0;
var variation = 0;
var montante = 0;
for (let counter = 1; counter <= period; counter++) {
montante = initialCash * (1 + (tax / 100)) ** counter;
diference = montante - initialCash;
variation = diference / initialCash * 100;
createBoxes(counter, period, montante, diference, variation);
montante += diference;
}
}, [initialCash, tax, period])
function createBoxes(c, p, m, d, v) {
box = (
<div className="card">
<p> Mês: {c}</p>
<p> Valor: {m}</p>
<p> Diferença:{d}</p>
<p> Variação:{v}</p>
</div>
);
Box(box);
}
Box.js:
import React from 'react';
export default function Box(props) {
//{ value } = props;
console.log(props.box);
return (
<div>{props.box}</div>
);
}
just create a listing or array state and when implementing this list automatically it creates the boxes, what their difficulty is?
– novic
When I run the code all are Undefined when they arrive at Box
– ajsalmeida