I read componentDidMount
gets called only once for initial rendering but I'm seeing it's getting rendered multiple times.
It seems I created a recursive loop.
- componentDidMount dispatches action to fetch data
- upon receiving the data, it fires success action to store the data in redux state.
- a parent react component is connected to redux store and has
mapStateToProps
for the entry that just changed in the above step - parent renders child components (which is programmatically selected via variable)
- the child component's componentDidMount gets called again
- it dispaches action to fetch data
I think that's what's happening. I may be wrong.
How can I stop the loop?
Here's the code for programmatically rendering child components.
function renderSubviews({viewConfigs, viewConfig, getSubviewData}) {
return viewConfig.subviewConfigs.map((subviewConfig, index) => {
let Subview = viewConfigRegistry[subviewConfig.constructor.configName]
let subviewData = getSubviewData(subviewConfig)
const key = shortid.generate()
const subviewLayout = Object.assign({}, subviewConfig.layout, {key: key})
return (
<div
key={key}
data-grid={subviewLayout}
>
<Subview
{...subviewData}
/>
</div>
)
})
}