Categories
React Answers

How to Concatenate JSX Elements into an Array?

Spread the love

Sometimes, we want to concatenate JSX elements into an array.

In this article, we’ll look at how to concatenate JSX elements into an array.

Concatenate JSX Elements into an Array

To concatenate JSX elements into an array, we can call the JavaScript array’s push instance method.

For instance, we write:

import React from "react";

export default function App() {
  const buffer = [];
  buffer.push(<div>A</div>);
  buffer.push(<div>B</div>);
  buffer.push(<div>C</div>);

  return <div>{buffer}</div>;
}

to define the buffer array.

Then we call push with different elements we want to put into it.

And then we render the buffer array’s contents on the screen by putting it in between the braces.

Now we should see:

A
B
C

displayed.

Conclusion

To concatenate JSX elements into an array, we can call the JavaScript array’s push instance method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

One reply on “How to Concatenate JSX Elements into an Array?”

Leave a Reply

Your email address will not be published. Required fields are marked *