I am using React with Babel (es6). I have a problem to solve circular dependency between my components.
I create a menu in which we have:
- ItemFactory
- Itemfolder
- ItemGeneric
- MoreTypesOfItems
ItemFactory just returns any of the other components based on the details passed.
ItemFoldermust be able to reuse ItemFactoryto create nested menu items.
Component ItemFactory, itemFactory.js (simplified):
import ItemFolder from './itemFolder'
...
function ItemFactory (props) {
switch (props.type) {
case 'link': return <ItemLink {...props} />
case 'blank': return <ItemBlank {...props} />
case 'folder': return <ItemFolder {...props} />
default: return <ItemGeneric {...props} />
}
}
modules.exports = ItemFactory
ItemFolder Component, itemFolder.js (simplified):
import ItemFactory from './itemFactory'
...
Items = props.items.map(item => {
return <ItemFactory {...item} />
})
module.exports = ItemFolder
As you can see, both require each other. This causes some circular dependency problem when I get an empty object.
Any help is appreciated :)