未定义'useState'no-undef React

时间:2020-09-02 12:15:21

标签: javascript reactjs react-hooks

我正在尝试使用React钩子解决一个简单的问题。我认为解决方案很愚蠢,但我看不到。 我试图在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
我得到这个错误

第7:29行:未定义'useState'no-undef

6 个答案:

答案 0 :(得分:15)

您需要导入“ useState”:

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

答案 1 :(得分:3)

您忘记了导入useState

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

答案 2 :(得分:2)

我发现您错过了useState导入。

您需要添加喜欢

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

或设为React.useState,即

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

答案 3 :(得分:1)

它需要导入才能在 React 中的钩子代码中工作。

import {useState} from "react";

或者,如果你已经从“react”导入了 React 并且不想单独导入 hook,那么你可以使用它,

React.useState()

答案 4 :(得分:1)

尝试导入:

从“./StateProvider”导入 {useStateValue}

创建一个名为“StateProvider.js”的新文件

插入此代码

import react, { createContext, useContext, useReducer } from "react";

export const StateContext = createContext();

export const StateProvider = ({ reducer, initialState, children }) => (
    <StateContext.Provider value={useReducer(reducer, initialState)}>
        {children}
    </StateContext.Provider>
);

export const useStateValue = () => useContext(StateContext);

答案 5 :(得分:0)

您需要导入“useState”:

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

enter image description here

enter image description here