I try to implement an component in my application. There i want to set the numbers of rendered paragraphs according to the numbers set by the user. For example if user selects 3 should appear 3 paragraphs, but if user after that select 1, the number of paragraphs should decrease to 1. This is my code:
const Nr = () => {
const [nr, setNr] = useState([]);
function onChange(value) {
console.log("changed", value);
setNr([...nr, value]);
}
return (
<div>
{nr.map(i => {
return <p>{i}</p>;
})}
<InputNumber min={1} max={10} onChange={onChange} />;
</div>
);
};
export default Nr;
But now if select 3 the paragraphs are genereted ok, but if after that i select 2, the paragraphs does not decrease, but increase. How to solve this?
demo: https://codesandbox.io/s/cranky-glitter-9d7sn?file=/Number.js:163-490