码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-13 19:13:18      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:nbsp   css   min   name   pos   rom   react   hat   const   

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

标签:nbsp   css   min   name   pos   rom   react   hat   const   

原文地址:https://www.cnblogs.com/Answer1215/p/10263475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!