Home > Net >  Problems with circular imports using index.js?
Problems with circular imports using index.js?

Time:08-05

Given the following file structure:

myFiles
├── index.js
├── getTrue.js
└── dependentGetFalse.js

And the following code

// index.js
export { getTrue } from './getTrue'
export { dependentGetFalse } from './dependentGetFalse'
// getTrue.js
export const getTrue = () => true
// dependentGetFalse.js
import { getTrue } from '.'
export const dependentGetFalse = () => !getTrue()

Where there's (what I assume to be) a circular import between dependentGetFalse.js and index.js.

What problems will arise from this? Or is it ok to have?

CodePudding user response:

If your codes run flawless and you are comfortable with them, It is ok to have this circular situation

CodePudding user response:

It's best to avoid using '.' imports.

Try this:

// dependentGetFalse.js
import { getTrue } from './getTrue'
export const dependentGetFalse = () => !getTrue()

index.js is useful when you try to import something from outside of this folder.

  • Related