React Native 究竟是什么<>(空)组件

2022-01-21 00:00:00 javascript components react-native jsx

在 React Native 中,您可以将一组组件封装在一个 <View>(或类似的)组件中.您还可以将一组组件封装为 <></>.这些是什么?他们只是转换为基本视图吗?这可能不是一个好的做法,但它不会发出警告,也不会崩溃.

In React Native you can encapsulate a set of components in one single <View> (or similar) component. You can also encapsulate a set of components as <> and </>. What are these? Do they just translate to an base View? It's probably not a good practice but it doesn't give a warning and it doesn't crash.

推荐答案

这是 Fragment 组件的 React 快捷方式.

It's the React shortcut for Fragment component.

你可以这样写:

import React, { Component } from 'react'

class Component extends Component {
  render() {
    return <> <ComponentA/> <ComponentB/> </>
  }
}

或者不用快捷键,导入Fragment组件

Or without the shortcut and import Fragment component

import React, { Component, Fragment } from 'react'

class Component extends Component {
  render() {
    return <Fragment> <ComponentA/> <ComponentB/> </Fragment>
  }
}

你必须知道,你不能使用任何快捷键或道具.

You have to know, you can't use any key or prop with the shortcut syntax.

这里是官方文档

希望对你有帮助!

相关文章