我只想指出您是两次从“反应”库中导入
// You have: (look closely at libs you are importing from and how)
import React from 'react';
import LoginForm from './loginForm'
import useState from "react"
// Should be:
import React,{ useState } from 'react';
import LoginForm from './loginForm'
// Why?
// Because useState is one of React libs' export default's apis / methods
// aka useState is just a part of the React default export but also is itself,an export
// React.useState() is how it would look if you just imported the React lib itself and nothing else
// how I personally handle any react apis is via ==>
import React,{ useState } from 'react
// apart from loving seeing all libraries and individual apis imported
// as soon as I see a file to get a sense of its potential functionalities,// read my detailed explanation below
在这里,您实际上是从整个React库中导入(export default react
,并简单地将其命名为随机字符串useState,然后尝试使用它来使用真正的React.useState()
api /方法。 / p>
因此,您尝试以这种方式像实际的React useState
api一样使用useState绝对会导致错误,因为您在导入默认导入的react lib和api时基本上是在说require('react')()
这是该默认导出的一部分,还是它本身是需要在import语句中从lib解构的导出,不确定是否相关,但您100%的代码格式错误,我无法相信没有人提到过?
再举一个例子,要使它按原样工作(尽管eslint现在应该在重复保存之前就对重复的导入进行尖叫),您将不得不执行useState.useState()
,这显然不是您想要的。有些人不介意React.useState()
,但我个人会避开哈哈jk,但会破坏进口!!!请(:
使用最佳编码标准 是成为团队中甚至是您自己的个人项目中优秀软件工程师的关键,这绝对会增加您的工作效率DX和整体输出。
希望这对我的兄弟有帮助。我们都经历了这些小怪癖,一旦您学习了这些小怪癖,就将它们添加到“我肯定知道的事情”列表中,并继续进行货运
,
请添加更多信息。就像您选择导入的API /模块一样,它是否使用
export default function
vs
export function
您如何消费?
您可能已将其导入为
import Something from './something';
您的something.js可能如下所示:
export function Something(){
}
,
将引发此错误的代码发布到文件中会很有用。
有了此信息,就有可能您将某些内容作为默认导入导入,但是没有任何模块从默认模块导出为默认导入。尝试调查默认出口,看看其中之一是否没有默认出口。您可以查阅模块文档。
,
如果您将 webpack 更新到版本 5.x.x,那么这可能与 output.library setting issue
因此,如果您在 webpack 输出中有库设置,请尝试将其删除:
output: {
path: path.resolve(__dirname,'build'),filename: 'painterro.commonjs2.js',library: 'Painterro',// ⏪ this is the issue
libraryTarget: 'commonjs2',},
,
在类似情况下,我在 Yarn 下运行 Next 时遇到了上述错误。当我添加一个新页面时,问题出现了。解决它的唯一方法是运行
yarn upgrade
。
本文链接:https://www.f2er.com/2992118.html