React里的Fragment標(biāo)簽的具體使用
react return里返回多個元素值要有父標(biāo)簽包裹。
React.Fragment組件能夠在不額外創(chuàng)建 DOM 元素的情況下,讓 render()方法中返回多個元素。相當(dāng)于空標(biāo)簽<></>。
<></>包裹多個元素↓:
import React, { Component, Fragment } from "react";
import "./App.css";
class App extends Component {
render() {
return (
<>
<div>
<input />
<button>按鈕</button>
</div>
<ul>
<li>哈</li>
<li>咯</li>
</ul>
</>
);
}
}
export default App;
使用Fragment標(biāo)簽↓:
和<></>實現(xiàn)效果一致。
import React, { Component, Fragment } from "react";
import "./App.css";
class App extends Component {
render() {
return (
<Fragment>
<div>
<input />
<button>按鈕</button>
</div>
<ul>
<li>哈</li>
<li>咯</li>
</ul>
</Fragment>
);
}
}
export default App;<React.Fragment>就不用在頭部引入了↓:
import React, { Component } from "react";
import "./App.css";
class App extends Component {
render() {
return (
<React.Fragment>
<div>
<input />
<button>按鈕</button>
</div>
<ul>
<li>哈</li>
<li>咯</li>
</ul>
</React.Fragment>
);
}
}
export default App;使用<div>包裹多個標(biāo)簽就會多一層嵌套↓:

import React, { Component, Fragment } from "react";
import "./App.css";
class App extends Component {
render() {
return (
<div>
<div>
<input />
<button>按鈕</button>
</div>
<ul>
<li>哈</li>
<li>咯</li>
</ul>
</div>
);
}
}
export default App;<></>和Fragment標(biāo)簽的區(qū)別
Fragment標(biāo)簽支持能接受鍵值或?qū)傩裕梢员闅v循環(huán)渲染元素
import React, { Component } from "react";
import "./App.css";
const list = [
{ id: 0, name: "鳳凰火", description: "最肉" },
{ id: 1, name: "彼岸花", description: "花花" },
];
class App extends Component {
render() {
return list.map((item) => (
<React.Fragment key={item.id}>
<li>
{item.name}是{item.description}
</li>
</React.Fragment>
));
}
}
export default App;
到此這篇關(guān)于React里的Fragment標(biāo)簽的具體使用的文章就介紹到這了,更多相關(guān)React Fragment標(biāo)簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nodejs和react實現(xiàn)即時通訊簡易聊天室功能
這篇文章主要介紹了nodejs和react實現(xiàn)即時通訊簡易聊天室功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
react中的useImperativeHandle()和forwardRef()用法
這篇文章主要介紹了react中的useImperativeHandle()和forwardRef()用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
React實現(xiàn)數(shù)字滾動組件numbers-scroll的示例詳解
數(shù)字滾動組件,也可以叫數(shù)字輪播組件,這個名字一聽就是非常普通常見的組件。本文將利用React實現(xiàn)這一組件,感興趣的小伙伴可以了解一下2023-03-03
基于Node的React圖片上傳組件實現(xiàn)實例代碼
本篇文章主要介紹了基于Node的React圖片上傳組件實現(xiàn)實例代碼,非常具有實用價值,需要的朋友可以參考下2017-05-05

