函数式组件是一个普通函数,接受参数并返回一个 React 元素。

一些特殊的返回值:

import React from 'react';
import { render } from 'react-dom';

const App = () => {
  return; // 等价于 return undefined;
};

render(<App />, document.getElementById('root'));

错误信息

错误提示我们没有返回任何内容供 render。

import React from 'react';
import { render } from 'react-dom';

const App = () => {
  return null;
};

render(<App />, document.getElementById('root'));

除此之外还有以下类型:

问题的原因在于 render(<App />, document.getElementById('root'));,也就是 react-dom 的 render 方法,它接受的第一个参数为 React 元素类型

源码中的定义为

function render(
  element: React$Element<any>,
  container: Container,
  callback: ?Function,
) {};

可以看到 element 的类型为 React$Element<any>,接收的为一个 React 组件。

tips😂: 这样做会有特殊的效果:

import React from 'react';
import { render } from 'react-dom';

const App = () => {
  return <App />;
};

render(<App />, document.getElementById('root'));