I have a Golf Scorecard that displays fine if I manually generate each row using divs but when I try and generate the divs in a data.map loop then it the formatting is all lost and doesnt display correctly.
Here is the code:
<div className="griditem"></div>
<div className="griditem">Total</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem yellow">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem red">
<input type="text" className="input" />
</div>
<div className="griditem red">
<input type="text" className="input" />
</div>
<div className="griditem fred">
<input type="text" className="input fred" />
</div>
This is how I put it in the data.map:
{data.map((item, index) => (
<div key={index}>
<div className="griditem"></div>
<div className="griditem">Total</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem yellow">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem">
<input type="text" className="input" />
</div>
<div className="griditem red">
<input type="text" className="input" />
</div>
<div className="griditem red">
<input type="text" className="input" />
</div>
<div className="griditem fred">
<input type="text" className="input fred" />
</div>
</div>
))}
2
Answers
Got the answer eventually from here --> react js issue with rendering data in array.map()
Because the resulting HTML is different. In the second example you’re wrapping all of the elements in an outer
<div>
which isn’t present in the first example:If your styling is targeting a specific structure, changing that structure will break that styling. Either update the styling, or don’t change the structure. You can wrap your elements in a React fragment instead: