为什么usestate()在react js中不起作用?
当我声明一个使用状态变量时,我的React应用程序停止工作。如果我删除美国,则代码可以正常工作。我不明白有什么问题?
import React from "react";
import { Helmet } from "react-helmet";
import { Link } from "react-router-dom";
import useState from "react";
import "./login.scss";
const Login = (props) => {
const [credentials, setCredentials] = useState({
user_name: '',
password: '',
})
return (
<div className="application">
</div>
);
}
export default Login;
when I declare a use State variable, my react app stops working.. if I uncomment the useState then the code works fine. I don't understand what is the problem?
import React from "react";
import { Helmet } from "react-helmet";
import { Link } from "react-router-dom";
import useState from "react";
import "./login.scss";
const Login = (props) => {
const [credentials, setCredentials] = useState({
user_name: '',
password: '',
})
return (
<div className="application">
</div>
);
}
export default Login;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里可能有一些错误:
USESTATE
?导入React,{usestate}来自“ React”
您是使用
setText
设置值还是设置默认状态值?您的当前示例将文本的默认状态设置为''
您是否在渲染方法中返回文本?例如
返回&lt; p&gt; {text}&lt;/p&gt;
There could be a few things wrong here:
useState
from react?import React, { useState } from "react"
Are you setting value using
setText
or setting a default state value? Your current example sets default state for text as''
Are you returning text in the render method? E.g.
return <p>{text}</p>
将卷曲牙套放在Usestate周围,您像这样导入的地方。
除此之外,一切看起来都很好!
您可能会删除这条线,
但不会改变任何东西:)
put curly braces around useState where you imported it like so..
other than that everything looks fine!
you can potentially delete the line with
but it's not gonna change anything :)
而不是:
写:
为什么我需要将
{}
添加到import
?答案:在使用
在用
默认>默认
关键字导出的内容上使用 - 您无需将变量名称包装在>中{}
。但是,当在导出 的东西上使用
import
不是 作为默认
时,您需要包装变量名称使用{}
,并准确地说出您要import
。Instead of:
Write:
Why do I need to add
{ }
to theimport
?Answer: When using
import
on something that was exported with thedefault
keyword - you don't need to wrap the variable name in{ }
.However, when using
import
on something that was exported not asdefault
, you are required to wrap the variable name with{ }
and say exactly what you want toimport
.