javascript - 如果组件具有 IIFE,则 React 应用程序无法编译
问题描述
下面是我的组件代码。
function App() {
!function() {
console.log("foo")
}()
return (
<div>APP</div>
)
}
export default App
构建时出现编译错误。
Creating an optimized production build...
Failed to compile.
src\App.js
Line 2:3: Expected an assignment or function call and instead saw an expression no-unused-expressions
Search for the keywords to learn more about each error.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
我不明白为什么会出现编译错误。这是有效的 JavaScript 代码。
我可以忽略这个错误吗?
实际上,我用它来使用 useEffect 中的异步功能。
useEffect(() => {
!async function() {
await something()
}()
})
解决方案
你能试试这个吗?
function App() {
true && function() {
console.log("foo")
}()
return !function(){console.log("poo")}
return (
<div>APP</div>
)
}
推荐阅读
- javascript - 获取所选文本框的计数 indesign
- python - 没有名为 tensorflow.contrib 的模块
- r - 在 R 中找到某一年的最接近值
- java - Hibernate 无法处理不同模式中具有相同名称的序列
- java - Quarkus:具有通用返回类型的 Rest 客户端
- jquery - JQuery 使用不可见的 Google Captcha 验证多个表单
- c# - 将 Canon EDSDK 错误代码翻译成错误信息
- bash - 带有 bash 的 Yaml:在字符串中转义引号
- sql - 在sql server中按组填充序列
- sql - Laravel Query Builder 中的第一个字母如何计算?