Can we pass state variable as an argument to external util function from react js function defined inside a component?

可紊 提交于 2021-01-29 14:51:07

问题


I need to pass state variable as a parameter to external function. I am trying to do something like this.

https://stackblitz.com/edit/react-vvdfx4?file=index.js

Expected to work like a slackblits link provided

It is working properly here but unable to figure out why it is throwing error in my project.

I am passing array of object as argument.

`GlobalHeader.js:340 Uncaught TypeError: Object(...) is not a function
at GlobalHeader.temp (GlobalHeader.js:340)
at GlobalHeader.<anonymous> (GlobalHeader.js:241)
at getStateFromUpdate (react-dom.development.js:16277)
at processUpdateQueue (react-dom.development.js:16338)
at mountClassInstance (react-dom.development.js:11519)
at updateClassComponent (react-dom.development.js:14688)
at beginWork (react-dom.development.js:15644)
at performUnitOfWork (react-dom.development.js:19312)
at workLoop (react-dom.development.js:19352)
at renderRoot (react-dom.development.js:19435)`

Actual Error code: Imports: import generateChartInputData from '../../utils/generateChartInputData';

Function call:

`temp(gd, saft, paramMonth, arrivalType) {
let c = generateChartInputData(gd, saft, paramMonth);
console.log(c);

}`

Function Definition:

function generateChartInputData(monthWiseData,arrivalType,selectedMOM){
console.log('-------------------------------');
console.log(monthWiseData,arrivalType,selectedMOM);
console.log('-------------------------------');
return 1;

}

export {generateChartInputData}`


回答1:


Replace:

import generateChartInputData from '../../utils/generateChartInputData'

with this if generateChartInputData is defined in generateChartInputData.js file:

import { generateChartInputData } from '../../utils/generateChartInputData'

I hope it help you.



来源:https://stackoverflow.com/questions/56252808/can-we-pass-state-variable-as-an-argument-to-external-util-function-from-react-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!