0
const jsx = []
for (var i = 0; i < 25; i++) { 
    var row = document.createElement('div');
    row.className = 'row row' + (i + 1);
    row.id = 'row' + (i + 1);
    for (var j = 0; j < 25; j++) {
        var node = document.createElement('div');
        node.className = 'node node' + ((i * 10) * (j + 1));
        node.id = 'node' + count;
        count++;
        row.appendChild(node);
    }
    jsx.push(row);
}    
  

return (
    <div className='maze'>
       {jsx}
    </div>
)
Error Cannot render  object use array instead

this code is running in javascript

Basically, I want 25 divs, and in each div 25 div children. Can you suggest react approach to do this?

1
  • Have you tried to follow the React tutorial? You'll figure out yourself. Commented Sep 14, 2021 at 8:06

2 Answers 2

1

In React you can just "describe" the HTML you want, i.e. <div>....</div>. There's no need really to create elements, that's what React does.

You could generate 2 arrays, each of length you need, and map them to the JSX you want.

Example 10x10:

const App = () => [...Array(10).keys()].map(row => (
  <div key={row}>
    (row {row})[
    {[...Array(10).keys()].map(col => (
      <span key={col}>[{col}]</span>
    ))}
    ]
  </div>
));

const rootElement = document.getElementById("root");
ReactDOM.render(
  <App />,
  rootElement
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="root" />

Sign up to request clarification or add additional context in comments.

Comments

1

You can write something like this

<div className=''>
        {new Array(25).fill(0).map(div => {
          <div className="">
            {new Array(25).fill(0).map(innerDiv => {
              <div className=""></div>
            })}
          </div>
        })}
        </div>

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.