gpt4 book ai didi

javascript - 'useState' 未定义 no-undef React

转载 作者:行者123 更新时间:2023-12-03 06:53:23 24 4
gpt4 key购买 nike

我正在尝试使用 react 钩子(Hook)来解决一个简单的问题。我相信解决方案很愚蠢,但我看不到。
我试图在我的 package.json 中查看,但没有找到解决方案。而且我很确定我正在以一种好的方式宣布我的状态。

import React, { useEffect } from "react";
import "./App.css";
import Chuck from "./gettyimages-83457444-612x612.jpg";
import axios from "axios";

function App() {
const [state, setState] = useState({
joke: "",
});

useEffect(() => {
fetchData();
}, []);

const fetchData = async () => {
const result = await axios.get("https://api.chucknorris.io/jokes/random");
console.log(result.data.value);
setState({ ...state, joke: result.data.value });
};
return (
<div className="container">
<div className="row">
<div className="col-6">
<h1 className="title">Chuck API</h1>
<img src={Chuck} alt="ChuckNoris" />
</div>
<div className="col-6 searchJokeCol">
<div className="card">
<div className="card-header">
<span>Search for the Phrase of Chuck</span>
</div>
<div className="card-body">
<input type="text"></input>
</div>
</div>
<div>
<button className="btn btn-warning btn-lg">Just CLICK!</button>
</div>
</div>
</div>
<h2 className="subTitle"> Here is The joke</h2>
<h4>{state.joke}</h4>
</div>
);
}

export default App;

这就是我的 package.json

  "dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"axios": "^0.20.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.3",
"react-test-renderer": "^17.0.0-rc.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"ej

我得到这个错误

Line 7:29: 'useState' is not defined no-undef

最佳答案

您需要导入“useState”:

import React, { useEffect, useState } from "react";

关于javascript - 'useState' 未定义 no-undef React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63705317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com