admin管理员组文章数量:1391969
I am trying to render empty ponent when my sections are empty. This is the sample code of mine
<SectionList
sections={[
{title: 'sectionOne', data: this.props.Activities.ActivityTypeOne},
{title: 'sectionTwo', data: this.props.Activities.ActivityTypeTwo},
{title: 'sectionThree', data: this.props.Activities.ActivityTypeThree}
]}
keyExtractor={ (item, index) => index }
stickySectionHeadersEnabled={true}
extraData={this.state}
ListEmptyComponent={this.renderEmptyScreens}
/>
But when this 3 all arrays are empty, it does not trigger the ListEmptyComponent
Can anyone tell me what is wrong with this code, or if my logic is incorrect can anyone please explain me.
Bacically I need to render some view when all 3 arrays are empty.
I am trying to render empty ponent when my sections are empty. This is the sample code of mine
<SectionList
sections={[
{title: 'sectionOne', data: this.props.Activities.ActivityTypeOne},
{title: 'sectionTwo', data: this.props.Activities.ActivityTypeTwo},
{title: 'sectionThree', data: this.props.Activities.ActivityTypeThree}
]}
keyExtractor={ (item, index) => index }
stickySectionHeadersEnabled={true}
extraData={this.state}
ListEmptyComponent={this.renderEmptyScreens}
/>
But when this 3 all arrays are empty, it does not trigger the ListEmptyComponent
Can anyone tell me what is wrong with this code, or if my logic is incorrect can anyone please explain me.
Bacically I need to render some view when all 3 arrays are empty.
-
Where do you get
sectionList
from? – Vencovsky Commented Jul 26, 2019 at 13:04 - I did not get it @vencovsky – sd_dewasurendra Commented Jul 26, 2019 at 13:09
-
Now with your edit, I see where you get it from. Please show the case where it's nothing working. Please show how is
But when this 3 all arrays are empty
– Vencovsky Commented Jul 26, 2019 at 13:12 - Your list isn't empty, it contains three sections. The fact that these sections are empty isn't relevant. – Etheryte Commented Jul 26, 2019 at 13:19
- this.props.Activities.ActivityTypeOne, this.props.Activities.ActivityTypeTwo, this.props.Activities.ActivityTypeThree when this lists are empty I need to display another view – sd_dewasurendra Commented Jul 26, 2019 at 13:20
2 Answers
Reset to default 5After running into this problem myself, I discovered that the sections
property has to be pletely empty for the list to be considered empty (aka, sections
itself should equal to []
).
Solution:
Use a ternary operator to choose what to pass into sections
based on your own criteria. If it's decided to be empty, pass in []
, else pass in your sections object. As an example, your code would look something like this:
isSectionsEmpty(sectionsObject){
//logic for deciding if sections object is empty goes here
}
render(){
/* ...
Other render code
...
*/
const sections = [
{title: 'sectionOne', data: this.props.Activities.ActivityTypeOne},
{title: 'sectionTwo', data: this.props.Activities.ActivityTypeTwo},
{title: 'sectionThree', data: this.props.Activities.ActivityTypeThree}
]
return(
...
<SectionList
sections={this.isSectionsEmpty(sections) ? [] : sections}
keyExtractor={ (item, index) => index }
stickySectionHeadersEnabled={true}
extraData={this.state}
ListEmptyComponent={this.renderEmptyScreens}
/>
...
)
}
Note: You could also forego the ternary operator and directly return either your sections object or []
from isSectionsEmpty
, but this method allows for you to check if sections is "empty" elsewhere in your code, if needed.
You could add a section footer ponent in which you will render your empty screen based on section.data.length
For a little example see this
本文标签: javascriptReact native list empty component not working in section listStack Overflow
版权声明:本文标题:javascript - React native list empty component not working in section list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744757575a2623553.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论