admin管理员组文章数量:1430967
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
1 Answer
Reset to default 4You 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));
}
}, []);
}
本文标签: javascriptReact functional components say its not a functionStack Overflow
版权声明:本文标题:javascript - React functional components- say its not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745578421a2664473.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论