Search code examples
reactjsfunctionreturnstate

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


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}`


Solution

  • 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.