freeCodeCamp/guide/chinese/react/fragments/index.md

60 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Fragments
localeTitle: 片段
---
# 片段
片段是在不使用包装元素的情况下呈现多个元素的方法。在JSX中尝试渲染没有封闭标记的元素时您将看到错误消息`Adjacent JSX elements must be wrapped in an enclosing tag` 。这是因为当JSX进行转换时它会创建具有相应标记名称的元素并且如果找到多个元素则不知道要使用哪个标记名称。在过去经常解决这个问题的方法是使用包装div来解决这个问题。然而React的第16版带来了`Fragment`的添加,这使得不再需要它。
`Fragment`扮演包装器而不向DOM添加不必要的div。您可以直接从React导入中使用它或者解构它
```jsx
import React from 'react';
class MyComponent extends React.Component {
render(){
return (
<React.Fragment>
<div>I am an element!</div>
<button>I am another element</button>
</React.Fragment>
);
}
}
export default MyComponent;
```
```jsx
// Deconstructed
import React, { Component, Fragment } from 'react';
class MyComponent extends Component {
render(){
return (
<Fragment>
<div>I am an element!</div>
<button>I am another element</button>
</Fragment>
);
}
}
export default MyComponent;
```
React版本16.2进一步简化了此过程允许将空的JSX标记解释为片段
```jsx
return (
<>
<div>I am an element!</div>
<button>I am another element</button>
</>
);
```
#### 更多信息:
* [React.Fragment官方文档](https://reactjs.org/docs/react-api.html#reactfragment)
* [React v16.2.0:改进了对碎片的支持](https://reactjs.org/blog/2017/11/28/react-v16.2.0-fragment-support.html)