0

I want to use warningItem within my return statement in order to map some data into a react component.

I want to loop over area but have problems with syntax.

createWarnings = warningsRawData => {
    return warningsRawData.map(warningItem => {
        return (
            <div>
                <p className={styles.warningMainText} />
                <p>warningItem.area[0]</p>
            </div>
        );
    });
};
3
  • Assuming styles is defined somewhere, this seems to be fine. Maybe warningItem doesn't have the structure you think it has? Commented Apr 6, 2017 at 19:45
  • @FelixKling nope. i can reach my data before the second return via warningItem.area[0]. Inside the return... nothing. Commented Apr 6, 2017 at 19:47
  • What exactly do you mean by "It does not get there though"? Commented Apr 6, 2017 at 19:51

2 Answers 2

3

It looks like you are missing the brackets around it. Try:

createWarnings = warningsRawData => {
    return warningsRawData.map( (warningItem, i) => {
        return (
            <div key={i}>
                <p className={styles.warningMainText} />
                <p>{warningItem.area[0]}</p>
            </div>
        );
    });
};
Sign up to request clarification or add additional context in comments.

3 Comments

Good catch... should be obvious though if warningItem.area[0] is literally printed on the page.
this works. I had a number of errors, but the i was a great addition. thnx!
@Spacemoose you're welcome :) I edited Elevine's answer to add that so it wouldn't have a warning of a missing key :)
1

Whenever you loop to return elememnt in react, add key attribute is must. Else you will get warning.And add {warningItem.area[0]}

createWarnings = warningsRawData => {
   let values  =  warningsRawData.map((warningItem,index) => {
        return (
            <div key={index}>
                <p className={styles.warningMainText} />
                <p>{warningItem.area[0]}</p>
            </div>
        );
    });
    return values
}

;

4 Comments

What did you change and why?
I added key={index}. Else it will throw warring.
And you think because of the warning the element is not rendered? Or why else would this solve the OP's problem?
thnx. i ll check it out.

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.