为什么使用Alert(Alerts)导出会导致错误,而使用Alert

  • 本文关键字:Alert 错误 Alerts javascript reactjs
  • 更新时间 :
  • 英文 :


在Brad的教程中,Alerts组件使用导出

export default withAlert(Alerts)

这是导致错误的原因:

The above error occurred in one of your React components:
in Unknown (created by App)
in Provider (created by App)
in Provider (created by App)
in App
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb me/react-error-boundaries to learn more about error boundaries. react-dom.development.js:21810:13
Error: Objects are not valid as a React child (found: object with keys {$$typeof, render}). If you meant to render a collection of children, use an array instead.
in Unknown (created by App)
in Provider (created by App)
in Provider (created by App)
in App 2 main.js line 753 > eval:14729:15

在阅读了react alerts文档后,导出被更改为以下内容,并且有效:

export default withAlert()(Alerts)

本质上:

  • 这个更改正确吗
  • 这个改变做了什么
  • 为什么有必要
  • 是。这是正确的
  • 由于react alerts的api发生了更改,您应该更新代码。此链接中描述了更改信息https://github.com/schiehll/react-alert/commit/9decd399fccc42c623bfdd44cd6fc381d75c3116
  • 遵循已发布的api,因为您使用了他们的代码:(。我猜他们在括号内添加了一些参数来自定义警报。但重要的是youtube中的指南使用了比当前版本更旧的版本

最新更新