简体   繁体   English

我正在尝试从 useEffect 挂钩调用 function 并且它给了我未定义

[英]I am trying to call a function from useEffect hook and it's giving me undefined

I am trying to call function 'handleSaveGeneral' from inside useEffect hook我正在尝试从 useEffect 钩子内部调用 function 'handleSaveGeneral'

const ProfilePage = (props) => {
  const [uneligible, setUneligible] = React.useState(false);

  useEffect(() => {
    const handleSaveGeneral = (e) => {
      var dateOfBirth = "2007-01-01";
      var split_dob = dateOfBirth.split("-");
      var month = split_dob[1];
      var day = split_dob[2];
      var year = split_dob[0];
      var dob_asdate = new Date(year, month, day);
      var today = new Date();
      var mili_dif = Math.abs(today.getTime() - dob_asdate.getTime());
      var age = mili_dif / (1000 * 3600 * 24 * 365.25);
      setUneligible(age < 18);
    };
  });

  return (
    <div>
      <Button variant="outline-primary" onClick={handleSaveGeneral}>
        Save
      </Button>
      {uneligible && (
        <Alert variant="filled" severity="error">
          This is an error alert — check it out!
        </Alert>
      )}
    </div>
  );
};

the 'save' Button also doesn't render for no reason, I am trying to show Alert when we click 'save' but the function 'handleSaveGeneral' is giving me undefined in the code sandbox of the problem “保存”按钮也不会无缘无故地呈现,当我们单击“保存”时,我试图显示警报,但是 function 的“handleSaveGeneral”在问题的代码沙箱中给了我未定义

https://codesandbox.io/s/modest-field-xqf91?file=/src/App.js:257-274 https://codesandbox.io/s/modest-field-xqf91?file=/src/App.js:257-274

const declaration is bloc-scope const声明是块作用域

So you should define the handleSaveGeneral function outside of useEffect and call it inside, so that other components can get access to it所以你应该在useEffect之外定义handleSaveGeneral useEffect并在里面调用它,以便其他组件可以访问它

update:更新:

Make sure you add an array to useEffect's dependency to avoid infinite loop确保将数组添加到 useEffect 的依赖项以避免无限循环

    const handleSaveGeneral = (e) => {
      var dateOfBirth = "2007-01-01";
      var split_dob = dateOfBirth.split("-");
      var month = split_dob[1];
      var day = split_dob[2];
      var year = split_dob[0];
      var dob_asdate = new Date(year, month, day);
      var today = new Date();
      var mili_dif = Math.abs(today.getTime() - dob_asdate.getTime());
      var age = mili_dif / (1000 * 3600 * 24 * 365.25);
      setUneligible(age < 18);
    };
  useEffect(() => {
handleSaveGeneral()
  },[]);

here is the whole code这是整个代码

import React, { useEffect } from "react";
import Button from "react-bootstrap/Button";
import Alert from "@material-ui/lab/Alert";

const ProfilePage = (props) => {
  const [uneligible, setUneligible] = React.useState(false);
  const handleSaveGeneral = (e) => {
    var dateOfBirth = "2007-01-01";
    var split_dob = dateOfBirth.split("-");
    var month = split_dob[1];
    var day = split_dob[2];
    var year = split_dob[0];
    var dob_asdate = new Date(year, month, day);
    var today = new Date();
    var mili_dif = Math.abs(today.getTime() - dob_asdate.getTime());
    var age = mili_dif / (1000 * 3600 * 24 * 365.25);
    setUneligible(age < 18);
  };
  useEffect(() => {
    handleSaveGeneral()
  },[]);

  return (
    <div>
      <Button variant="outline-primary" onClick={handleSaveGeneral}>
        Save
      </Button>
      {uneligible && (
        <Alert variant="filled" severity="error">
          This is an error alert — check it out!
        </Alert>
      )}
    </div>
  );
};

export default ProfilePage;

暂无
暂无

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

相关问题 我正在尝试在 class 的帮助下打印已定义多边形的周长,但它给了我未定义为 output 请告诉我问题出在哪里 - I am trying to print perimeter of defined polygons with the help of class but it is giving me undefined as an output please tell me where the problem 我正在尝试在JavaScript中添加三个数字的和,但这给了我NaN。 为什么? - I am trying to add the sum of three numbers in JavaScript but it's giving me NaN. Why? 我目前正在阅读 Eloquent JavaScript 并且我正在尝试解决递归测试,但是我的代码一直给我一个“未定义”的反馈 - I am currently reading Eloquent JavaScript and I'm trying to solve the recursion test, but my code keeps giving me an 'undefined' feedback 在我调用函数返回的数组后,它给了我“undefined” - After I call an array returned from function, it give me “undefined” 我正在尝试做这个问题,但是 state 给我带来了问题 - I am trying to do this question but, state is giving me problems 我只是想将函数 func() 中的一些 javascript 值传递给标签“p”。但它没有发生。谁能帮我解决这个问题? - I am just trying to pass some javascript value from function func() to the tag "p".But it's not happening.Can anyone help me with this? 我正在尝试检查未定义但无法正常工作 - I am trying to check for undefined but it's not working Ajax调用给了我一个不确定的错误? - Ajax call giving me an undefined - undefined error? 从 useEffect 钩子中的 axios 调用更新状态 - Update state from axios call in useEffect hook 在 React 本机 useEffect 中,使用异步调用我得到一个未定义的,在以下情况下如何避免未定义? - In React native useEffect ,using async call i am getting a undefined , How to avoid that undefined in the below case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM