0

I have working code and a little problem with the if statement in the map function here is code.

<main className="grid-b">
    {offers.map((offers,index)=> {
        if(offers.status==='pending')
        return
        <
        <article>
            <div className="text-b">
                <h3>Post ID: {index + 1}</h3>
                <p>Quantity (Kg): {offers.quantity}</p>
                <p>Unit Price (Rs): {offers.value}</p>
                <p>Expiry Date: {moment(offers.expiryDate).fromNow()}</p>
                <p>Offer Gives: {moment(offers.offerCreatedAt).fromNow()}</p>
            </div>
        </article>
        />
    })}
</main>

Now I want to check if the statement to render only when in map offers.status==='pending' if not, does not show anything. But this code does not work. How should I solve this issue?

3 Answers 3

4

You are using the same variable name (offers) in both the map and the parent function which is likely the issue. Change the map variable to something like offer and it should work as intended. You also have a typo in your mapped render.

e.g.

<main className="grid-b">
    {offers.map((offer,index)=> {
        if(offer.status==='pending')
        return (
          <article>
            <div className="text-b">
                <h3>Post ID: {index + 1}</h3>
                <p>Quantity (Kg): {offer.quantity}</p>
                <p>Unit Price (Rs): {offer.value}</p>
                <p>Expiry Date: {moment(offer.expiryDate).fromNow()}</p>
                <p>Offer Gives: {moment(offer.offerCreatedAt).fromNow()}</p>
            </div>
          </article>
        );
    })}
</main>
Sign up to request clarification or add additional context in comments.

Comments

1

You are not supposed to use the same name in the arrow function. Note the following changes:

<main className="grid-b">
    {offers.map((offer,index)=> 
        offer.status==='pending' &&(
        <article>
            <div className="text-b">
                <h3>Post ID: {index + 1}</h3>
                <p>Quantity (Kg): {offers.quantity}</p>
                <p>Unit Price (Rs): {offers.value}</p>
                <p>Expiry Date: {moment(offers.expiryDate).fromNow()}</p>
                <p>Offer Gives: {moment(offers.offerCreatedAt).fromNow()}</p>
            </div>
        </article>
    ))}
</main>

Comments

1

Also you can filter the offers list.

offers.filter(offer => offer.status==='pending').map((offer,index)=> {

}

Comments

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.