getResource() in a nested for loop and props not updated with fetched data
See original GitHub issueHi
Please I have this issue when fetching Layout Builder, I have the following code.
export default function LB(props){
const LB: Array<any> = props.LBPage.layout_builder__layout;
/*const pageStructure = {
sectionsNum: LB.length,
sectionsSetting: [],
contentTypes: [],
};
// Add each section setting to the pageStructure
LB.forEach(section => {
const sectionSetting = {
columns: null,
column_widths: section.section.layout_settings.column_widths,
context_mapping: section.section.layout_settings.context_mapping,
label: section.section.layout_settings.label,
};
sectionSetting.columns = sectionSetting.column_widths ? sectionSetting.column_widths.split('-').length : null;
pageStructure.sectionsSetting.push(sectionSetting);
});*/
console.log(LB, props.resource, props.resource[1], props);
return (
<Fragment>
{
LB.map(section => {
console.log(props.resource);
return <section className={
section.section.layout_settings.column_widths
?
section.section.layout_settings.column_widths.split('-').length + '-col' + ' ' + 'col-' +section.section.layout_settings.column_widths
:
'1-col'
}
>
</section>;
})
}
</Fragment>
);
}
export async function getStaticProps(){
let resource = [];
let resources = [];
const LBPage = await getResource('node--page', '6acb560b-f999-4011-9bab-160856d21f9f')
LBPage.layout_builder__layout.forEach((section, i) => {
resource.push({section: []});
//console.log('sections: ', section.section.components)
// console.log('section.section.components: ', section.section.components, section.section);
section.section.components.forEach(async (comp, j) => {
//console.log('resource[i]: ', i, j);
//console.log('component: ', comp.provider);
//console.log('params: ', comp.configuration.provider + "--basic", comp.configuration.id, comp.configuration.id.replace(comp.configuration.provider + ':', ''));
if(comp.configuration.provider === 'block_content'){
const nodeType = comp.configuration.provider + "--basic";
const nodeUid = comp.configuration.id.replace(comp.configuration.provider + ':', '');
resources.push('response');
const blocks = await getResource(nodeType, nodeUid);
console.log(i, 'Response: ', resource[i].section, blocks);
//resource[i].section.push(blocks);
setTimeout(() => {
resource[i].section.push(blocks);
}, 15000);
resource[i].section.push('blocks');
resources.push(blocks);
}
});
});
return {
props: {
LBPage,
resource,
resources,
},
};
}
LBPage fetched normally but resource has empty arrays here is it hen logged on the devtool
resource: [
{
"section": []
},
{
"section": []
},
{
"section": []
},
{
"section": []
}
]
every section there should has it’s own blocks inside the array, by the way, the blocks fetched has been logged on the server nodejs but not passed to props
resources is just to know whether the block inside the if condition
work or not
Thanks in advance.
Issue Analytics
- State:
- Created a year ago
- Comments:7
Top Results From Across the Web
Displayed data does not update in nested loop on state
When I press one of these buttons (that should add a row to rows array) the formData state does update, but the displayed...
Read more >How to Implement a Component Loop in React | Pluralsight
This guide demonstrates how to implement loops in common use cases, such as rendering a list of static data and outputting data from...
Read more >Fix list for IBM WebSphere Application Server V8.5
IBM WebSphere Application Server provides periodic fixes for the base and Network Deployment editions of release V8.5. The following is a complete listing ......
Read more >Documentation - SolidJS · Reactive Javascript Library
data works like a normal signal getter: use data() to read the last returned ... When nested the state you are updating may...
Read more >Reference Documentation - Spring
Shutting down the Spring IoC container gracefully in non-web applications. 3.6.2. ... getObject(); } @Bean public DataSource dataSource() { return new ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi
@shadcn Yes I use that layout builder patch
Okay thanks a lot, I will test what you say and come back to you with the result
Thanks a lot for both of you.
You’re probably using the layout builder patch.
@soft4net is right. You should wrap the top
forEach
in aPromise.all
.You could also use a
for ... of ...