0

I'm building my own invoice/estimate generator with React.

I want to have a number of lines "product" equivalent to my state.

I wanted to do a loop and return my JSX. But I have no error, but my line product appears once only, and my state is set to 2.

Here is my try :

export default function ProductInformations(props: TProductInformations) {
    const [numberOfProducts, setNumberOfProducts] = useState<number>(2);

    const handleNumberOfProducts = (type: string) => {
        if (type === "add") return setNumberOfProducts(numberOfProducts + 1);
        if (type === "remove") return setNumberOfProducts(numberOfProducts - 1);
    };

    for (let i = 0; i <= numberOfProducts; i++) {
        return (
            // here is <p> with "price", "quantity", "name of the product"...
        );
    }
}

As the JSX is a bit long, I don't want to do a function that returns html and call the function in JSX.

Any idea why ?

6
  • Does this answer your question? Does return stop a loop? Commented Jan 22, 2023 at 17:52
  • Check javascript basics Commented Jan 22, 2023 at 17:52
  • nope. I understand the JavaScript part. It is the return of the JSX Commented Jan 22, 2023 at 17:53
  • stackoverflow.com/questions/43047575/… Commented Jan 22, 2023 at 17:54
  • 1
    I know how to loop. Read the last phrase in my post Commented Jan 22, 2023 at 17:54

1 Answer 1

0

Either push to an array

export default function ProductInformations(props: TProductInformations) {
    const [numberOfProducts, setNumberOfProducts] = useState<number>(2);

    const handleNumberOfProducts = (type: string) => {
        if (type === "add") return setNumberOfProducts(numberOfProducts + 1);
        if (type === "remove") return setNumberOfProducts(numberOfProducts - 1);
    };
    
    const products =[]
  
    for (let i = 0; i <= numberOfProducts; i++) {
        products.push(
            // here is <p> with "price", "quantity", "name of the product"...
        );
    }
  
    return <>{products}</>
}

or use map

export default function ProductInformations(props: TProductInformations) {
    const [numberOfProducts, setNumberOfProducts] = useState<number>(2);

    const handleNumberOfProducts = (type: string) => {
        if (type === "add") return setNumberOfProducts(numberOfProducts + 1);
        if (type === "remove") return setNumberOfProducts(numberOfProducts - 1);
    };
    
  
    return [...Array(numberOfProducts + 1)].map(() => /* here is <p> with "price", "quantity", "name of the product"... */)

}

Resources:

Sign up to request clarification or add additional context in comments.

1 Comment

So I guess map is the only way ? I can't just tell React to create this HTML element state number of times ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.