javascript - React functional components- say its not a function - Stack Overflow

i am using react functional ponents (infact learning hooks ), so i am facing one issue when calling a f

i am using react functional ponents (infact learning hooks ), so i am facing one issue when calling a function with in another function

here is my code

my functional ponent

    const LiteraryPage = props => {
      if (Object.keys(mainData).length === 0 && mainData.constructor === Object) {
        setMainDataSection(validateData(mainData));
      }
      const validateData = async (data) => {
    let finalDataArray = [];
    if (data.cars !== undefined) {
      finalDataArray = finalDataArray.concat(data.cars);
    }
    if (data.mobiles !== undefined) {
      finalDataArray = finalDataArray.concat(data.mobiles);
    }
    if (data.scooters !== undefined) {
      finalDataArray = finalDataArray.concat(data.scooters);
    }
    return finalDataArray;
  };

 
    }

But am getting a error

Uncaught TypeError: validateData is not a function

Please correct me if am missing something or if am wrong at some place.

i am using react functional ponents (infact learning hooks ), so i am facing one issue when calling a function with in another function

here is my code

my functional ponent

    const LiteraryPage = props => {
      if (Object.keys(mainData).length === 0 && mainData.constructor === Object) {
        setMainDataSection(validateData(mainData));
      }
      const validateData = async (data) => {
    let finalDataArray = [];
    if (data.cars !== undefined) {
      finalDataArray = finalDataArray.concat(data.cars);
    }
    if (data.mobiles !== undefined) {
      finalDataArray = finalDataArray.concat(data.mobiles);
    }
    if (data.scooters !== undefined) {
      finalDataArray = finalDataArray.concat(data.scooters);
    }
    return finalDataArray;
  };

 
    }

But am getting a error

Uncaught TypeError: validateData is not a function

Please correct me if am missing something or if am wrong at some place.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 7, 2020 at 6:56 midhun kmidhun k 5761 gold badge14 silver badges36 bronze badges 8
  • 1 validateData, should be defined before you use it. Just change the order. – Julio Vásconez Commented Apr 7, 2020 at 7:00
  • 2 Is it possibly plaining about the function being used before it was defined? – Drew Reese Commented Apr 7, 2020 at 7:00
  • @JulioVásconez i changed the order, that time am getting some infinite loop error – midhun k Commented Apr 7, 2020 at 7:01
  • @DrewReese ^^. please see above ment – midhun k Commented Apr 7, 2020 at 7:01
  • 1 Maybe if you use an editor like vscode your code would format properly and tell you you can't use validateData before it's defined as Julio already mentioned. – HMR Commented Apr 7, 2020 at 7:44
 |  Show 3 more ments

1 Answer 1

Reset to default 4

You are possibly setting a state directly inside the functional ponent when you are calling validate data and setMainDataSection.

So Firstly you need to define validateData before using it and secondly, you need to execute your check in a useEffect

const LiteraryPage = props => {
  const validateData = async (data) => {
    let finalDataArray = [];
    if (data.cars !== undefined) {
      finalDataArray = finalDataArray.concat(data.cars);
    }
    if (data.mobiles !== undefined) {
      finalDataArray = finalDataArray.concat(data.mobiles);
    }
    if (data.scooters !== undefined) {
      finalDataArray = finalDataArray.concat(data.scooters);
    }
    return finalDataArray;
  };
  useEffect(() => {
    if (Object.keys(mainData).length === 0 && mainData.constructor === Object) {
      setMainDataSection(validateData(mainData));
    }
  }, []);

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745578421a4634107.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信