Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

javascript - 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

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

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

question from:https://stackoverflow.com/questions/65886740/i-am-trying-to-call-a-function-from-useeffect-hook-and-its-giving-me-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

const declaration is bloc-scope

So you should define the handleSaveGeneral function outside of useEffect and call it inside, so that other components can get access to it

update:

Make sure you add an array to useEffect's dependency to avoid infinite loop

    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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...