为什么usestate()在react js中不起作用?

发布于 2025-02-13 09:31:03 字数 484 浏览 1 评论 0原文

当我声明一个使用状态变量时,我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人│生佛魔见 2025-02-20 09:31:03

这里可能有一些错误:

  1. 您是否从React导入USESTATE

导入React,{usestate}来自“ React”

  1. 您是使用setText设置值还是设置默认状态值?您的当前示例将文本的默认状态设置为''

  2. 您是否在渲染方法中返回文本?例如

返回&lt; p&gt; {text}&lt;/p&gt;

  1. 您是否使用React V16.8或更高?如果没有的话,将不可用

There could be a few things wrong here:

  1. Are you importing useState from react?

import React, { useState } from "react"

  1. Are you setting value using setText or setting a default state value? Your current example sets default state for text as ''

  2. Are you returning text in the render method? E.g.

return <p>{text}</p>

  1. Are you using React v16.8 or greater? If not hooks will not be available
听不够的曲调 2025-02-20 09:31:03

将卷曲牙套放在Usestate周围,您像这样导入的地方。

import { useState } from "react";

除此之外,一切看起来都很好!

您可能会删除这条线,

import React from "react";

但不会改变任何东西:)

put curly braces around useState where you imported it like so..

import { useState } from "react";

other than that everything looks fine!

you can potentially delete the line with

import React from "react";

but it's not gonna change anything :)

糖粟与秋泊 2025-02-20 09:31:03

而不是:

import useState from "react";

写:

import { useState } from "react";

为什么我需要将{}添加到import

答案:在使用在用默认>默认关键字导出的内容上使用 - 您无需将变量名称包装在>中{}

但是,当在导出 的东西上使用import不是 作为默认时,您需要包装变量名称使用{},并准确地说出您要import

Instead of:

import useState from "react";

Write:

import { useState } from "react";

Why do I need to add { } to the import?

Answer: When using import on something that was exported with the default keyword - you don't need to wrap the variable name in { }.

However, when using import on something that was exported not as default, you are required to wrap the variable name with { } and say exactly what you want to import.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文