Holkan
Forum Replies Created
-
Forum: Fixing WordPress
In reply to: Getting current post’s featured image and its alt attribute@swisspidy
Thanks for your reply. I’m using withSelect and pretty much what you told me and now I get the current featured image alt attribute, but, I’m not building a bock, but a plugin withregisterPlugin();.
In this plugin I’m updating some post meta values withwp.apiRequest()insidegetDerivedStateFromProps(). The problem is that I can’t setState insidegetDerivedStateFromProps()sincethisis not accessible. Hence, I’m trying to setState outside that method. I thought that it can be done insidecomponentDidUpdate()but even if i wrapsetStateinside a condition, it fires an infinite loop.
Where can I setState in this case?componentDidUpdate(prevProps){ console.log('did update'); console.log(prevProps); if(prevProps.media !== undefined && prevProps.content_count !== ''){ let color_code = ''; let fti_color_code = ''; let alt_text = prevProps.media.alt_text; let content_count = prevProps.content_count; if(content_count < 300){ color_code = 'red'; }else if(content_count >= 300 && content_count < 400){ color_code = 'orange'; }else{ color_code = 'green'; } let alt_text_arr = alt_text.split(' '); if(alt_text_arr.length !== 0){ if(alt_text_arr.length < 4 && alt_text_arr.length > 6){ fti_color_code = 'red'; console.log(<code>fti _cc: ${fti_color_code}</code>); }else if(alt_text_arr.length >= 4 && alt_text_arr.length <= 6){ fti_color_code = 'green'; console.log(<code>fti cc: ${fti_color_code}</code>); } } if(this.state.body_content.color_code !== color_code || this.state.alt_attribute.color_code !== fti_color_code){ console.log(<code>content count: ${content_count}</code>); console.log(<code>alt text: ${alt_text}</code>); this.setState({ body_content: { color_code: {key: 'body_content_cc', value: color_code} }, alt_attribute:{ color_code: {key: 'alt_attribute_cc', value: fti_color_code} } }); } } }//End component did updateThanks for your help