[React] Return a list of elements from a functional component in React

Posted answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[React] Return a list of elements from a functional component in React相关的知识,希望对你有一定的参考价值。

We sometimes just want to return a couple of elements next to one another from a React functional component, without adding a wrapper component which only purpose would be to wrap elements that we want to render.

In this one minute lesson we are going to learn how to solve this problem by returning an array of components and as such - avoid adding unnecessary div wrappers.

 

Way 1:

import React from "react";
import "./App.css";

const App = () => {
  return (
    <>
      <div className="box">One</div>,
      <div className="box">Two</div>,
      <div className="box">Three</div>
    </>
  );
};

export default App;

 

Way 2:

import React from "react";
import "./App.css";

const App = () => {
  return [
    <div className="box">One</div>,
    <div className="box">Two</div>,
    <div className="box">Three</div>
  ];
};

export default App;

 

以上是关于[React] Return a list of elements from a functional component in React的主要内容,如果未能解决你的问题,请参考以下文章

A Bite Of React Component, Props and State

Range of Intervals

876. Middle of the Linked List

leetcode-mid-Linked list-17. Letter Combinations of a Phone Number

[LeetCode] 876. Middle of the Linked List

599. Minimum Index Sum of Two Lists