简体   繁体   English

如何在 React 中的 function 中获取 useEffect?

[英]how to get an useEffect inside a function in React?

I'm trying to make an countdown website for a project.我正在尝试为一个项目制作一个倒计时网站。 I want that the countdown is triggered by a button.我希望倒计时由按钮触发。 I'm using React but i keep getting this error我正在使用 React,但我不断收到此错误

React Hook "useEffect" is called in function "countdown" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter React Hook“useEffect”在 function“倒计时”中被调用,它既不是 React function 组件,也不是自定义 React Hook function。React 组件名称必须以大写字母开头

I'm pretty new to react, can somebody give me some advice to fix the problem?我对反应很陌生,有人可以给我一些建议来解决这个问题吗?

the code:代码:

import './App.css';
import React, { useEffect, useState } from 'react';

function App() {
  const time = 20;
  const [count, setCount] = useState(time);

  const startCount = () => {
    useEffect(() => {
      if (count > 0) {
        setTimeout(() => setCount(count - 1), 1000);
      } else {
        setCount('Times up');
      }
    });
  };

  return (
    <div className="App">
      <div>{count}</div>
      <button onClick={startCount}> start </button>
      <button> pauze </button>
    </div>
  );
}

export default App;

You can use setInterval instead of using setTimout您可以使用setInterval而不是使用setTimout

Fix your code to this:将您的代码修复为:

import './App.css';
import React, { useEffect, useState } from 'react';

function App() {
  const time = 20;
  const [count, setCount] = useState(time);
  const [isStart, setIsStart] = useState(false);


    useEffect(() => {
      if (isStart) {
        const timer = setInterval(() => {
          if (count > 0) {
            setCount(count - 1)
          } else {
            setCount('Times up');
            clearInterval(timer);
          }
        }, 1000);
      }
    }, [isStart]);


  return (
    <div className="App">
      <div>{count}</div>
      <button onClick={() => setIsStart(true)}> start </button>
      <button> pauze </button>
    </div>
  );
}

export default App;

You're trying to add useEffect inside a plain javascript function, that is why it throws an error that the useEffect you called is in a function that is not a React function component. You're trying to add useEffect inside a plain javascript function, that is why it throws an error that the useEffect you called is in a function that is not a React function component.

useEffect hook don't use in any child function of body component function useEffect 钩子不要在身体组件 function 的任何孩子 function 中使用

 import React, { useEffect, useState, useCallback } from 'react'; function App() { const [count, setCount] = useState(); useEffect(() => { if (count > 0) { setTimeout(() => setCount(count - 1), 1000); } else { setCount('Times up'); } }, [count]); return ( <> <div>{count}</div> <button onClick={() => setCount(20)}> start </button> <button> pauze </button> </>) } export default App;

useEffect should not be put inside a function. useEffect 不应放在 function 内。 You do not need that start count function.您不需要开始计数 function。 onClick can update a state, and let useEffect listen to the change of that state. onClick可以更新一个state,让useEffect监听那个state的变化。

Rules for hooks:钩子规则:

✅ Don't call Hooks from regular JavaScript functions. ✅ 不要从常规的 JavaScript 函数调用 Hooks。

✅ Call Hooks from React function components. ✅ 从 React function 组件调用 Hooks。

✅ Call Hooks from custom Hooks. ✅ 从自定义 Hooks 调用 Hooks。

You are calling hook ( useEffect ) from a plane javascript function which is against the rules of hooks.您正在从平面 javascript function 调用钩子( useEffect ),这违反了钩子规则。

You should not call hooks in plain js functions, you can solve your issue like this你不应该在普通的js函数中调用钩子,你可以像这样解决你的问题

import './App.css';
import React, { useEffect, useState } from 'react';

function App() {
  const time = 20;
  const [count, setCount] = useState(time);
  const [start, setStart] = useState(false)

  useEffect(() => {
    if (start) {
      if (count > 0) {
        setTimeout(() => setCount(count - 1), 1000);
      } else {
        setCount('Times up');
      }
    }

  }, [count, start])

  const startCount = () => {
    setStart(true)
  }

  return (
    <div className="App">
      <div>{count}</div>
      <button onClick={startCount}> start </button>
      <button> pauze </button>
    </div>
  );
}

export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM