0

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>
      ))}

How the formatting should appear

how the formatting appears in a data.map

0

2 Answers 2

0

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:

{data.map((item, index) => (
  <div key={index}>
    {...}
  </div>
)}

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:

{data.map((item, index) => (
  <React.Fragment key={index}>
    {...}
  </React.Fragment>
)}
Sign up to request clarification or add additional context in comments.

2 Comments

If i hadnt put the original in a <div key={index}> originally I got an error. How would I update the styling above rather than using React.Fragment?
@mvarta: However your styling is defined, you'd change it to match the new structure.
0

Got the answer eventually from here --> react js issue with rendering data in array.map()

 {data.map((item, index) => (
 <React.Fragment>
  </React.Fragment>
      ))}

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.