Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Identifier expected JSX for loop Eslint error

im new to react and try to build some code in JSX. but when i trying to automate the tags using for loop it give me a Eslint error.

<table className="TableData">

    
    
let num= 9;



  <thead>
    <tr>
      <th>Week</th>


       for(let i=0; i <num; i++){
        return <th> {i+1}</th>;
        } 
     

      <th>Win</th>
      <th>Lose</th>
    </tr>
  </thead>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A common approach to render some JSX for each item in an array is to use Array.map.

If you only have a number but no array, you can still create an array with new Array(9).fill(null)

export default function App() {
  let num = 9;
  return (
    <div className="App">
      <table className="TableData">
        <thead>
          <tr>
            <th>Week</th>
            {new Array(num).fill(null).map((_, i) => {
              return <th>{i}</th>;
            })}
            <th>Win</th>
            <th>Lose</th>
          </tr>
        </thead>
      </table>
    </div>
  );
}

Edit cool-snow-7hn5tq

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading