如何摆脱 React 应用程序中的 "Functions are not valid as a React child" 警告

How to get rid of "Functions are not valid as a React child" warning in React app

我是 React 的新手,我正在 Hooks.
的帮助下在 React 中开发简单的 CRUD 应用程序 该应用程序有两个表单组件 AddUserFormEditUserForm,它们相应地添加和编辑用户列表。还有一个显示用户列表的组件(但我没有在那里收到警告)。
一切正常,但我在浏览器的控制台中收到以下警告消息:
Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.

下面是我在 App.js 文件中渲染子组件的方式。 (我用红色箭头标记了我有警告的行)

更新 这是我在 AddUserForm 组件中处理表单的方式:

return (
 <Fragment>
  <form> // line 15, here I have warning as well
    onSubmit=
    {event => {
      event.preventDefault();
      if (!user.name || !user.username) return;

      props.addUser(user);
      setUser(initialFormState);
    }}
    <label>Name</label>
    <input type='text' name='name' value='' onChange={handleInputChange} />
    <label>Username</label>
    <input
      type='text'
      name='username'
      value=''
      onChange={handleInputChange}
    />
    <button>Add new user</button>
  </form>
 </Fragment>
);

我检查了一些解决方案,例如 ,但其中 none 对这个问题很有帮助。
你能告诉我我做错了什么吗?

我认为这只是一个错字:

<form> // You just closed form element, so 
onSubmit=
{event => { // this is a function returned as a React child
  event.preventDefault();
  if (!user.name || !user.username) return;

  props.addUser(user);
  setUser(initialFormState);
}}
<label>Name</label>
<input type='text' name='name' value='' onChange={handleInputChange} />
<label>Username</label>
<input
  type='text'
  name='username'
  value=''
  onChange={handleInputChange}
/>
<button>Add new user</button>

应该是这样的:

<form // Note the missing >
onSubmit=
{event => { // this is a function returned as a React child
  event.preventDefault();
  if (!user.name || !user.username) return;

  props.addUser(user);
  setUser(initialFormState);
}}> // form opening tag closed here

这实际上不是 React 问题,而是无效 HTML 导致 React 抛出错误,因为它无法解析和呈现 JSX。