SectionList doesn't work right with MobX
See original GitHub issueI am using SectionList with MobX (React-MobX). Then I get lots of warnings from mobx says: [mobx.array] Attempt to read an array index(2) that is out of bounds (2). Please check length first. Out of bound indices will not be tracked by MobX
(50 times) and [mobx.array] Attempt to read an array index(4) that is out of bounds (4). Please check length first. Out of bound indices will not be tracked by MobX
(6 times) with the code below.
I found it may be a bug for SectionList, cause if I commented SectionList rows, it’s ok.
Code here:
import React from 'react';
import {
View,
Text,
SectionList,
StyleSheet,
} from 'react-native';
import {observer,inject} from 'mobx-react';
import {observable,autorun,action} from 'mobx';
let data = [
{month: '五月', data: [{
key:"1",
date:"27日",
time:"08:42"
},{
key:"2",
date:"28r",
time:"08:42"
}]},
{month: '六月', data: [{
key:"3",
date:"21日",
time:"08:42"
},{
key:"4",
date:"21r",
time:"18:42"
},{
key:"5",
date:"21r",
time:"18:42"
},{
key:"6",
date:"21r",
time:"18:42"
}]},
];
@observer
export default class List extends React.Component{
@observable refreshing = false;
@observable pagination = {
page:1,
pageSize:10,
}
@observable listData = [];
@action refresh = ()=>{
console.warn("refreshed");
this.pagination.page = 1;
this.refreshing = true;
setTimeout(action(()=>{
this.refreshing = false;
this.listData=data;
}),500);
}
@action loadMore = ({distanceFromEnd})=>{
}
componentDidMount(){
this.refresh();
}
render(){
return (
<View style={styles.flex1}>
<Text>{JSON.stringify(this.listData)}</Text>
<SectionList
onRefresh={this.refresh}
refreshing={this.refreshing}
onEndReached={this.loadMore}
renderSectionHeader={({section})=><Text>{section.month}</Text>}
renderItem={({item})=><Text>1</Text>}
sections={this.listData}
/>
</View>
)
}
}
const styles = StyleSheet.create({
flex1:{
flex:1,
},
})
Environment
react-native -v
: react-native-cli: 2.0.1 react-native: 0.46.0node -v
: v7.10.0npm -v
: 3.10.9 mobx: 3.2.1 , mobx-react: 4.2.2
- Target Platform: Android (IOS not tested yet)
- Development Operating System: Win10
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7
Top Results From Across the Web
React component does not react to mobx observable data
On the component side, when the fetchingListOne is true, the ActivityIndicator displays, and in the ListHeaderComponent it should display true.
Read more >How I built my first React Native app for my first freelance client
I recently launched my first native mobile app built with React Native. As it happens, it was also the first app I've built...
Read more >8 ways to optimize React native FlatList performance
8 ways to optimize React native FlatList performance · 1. Avoid arrow functions inline for renderItem · 2. Don't use 1080P HD images...
Read more >React integration - MobX
While MobX works independently from React, they are most commonly used ... For observer to work, it doesn't matter how the observables arrive...
Read more >To pass MobX's observable array to FlatList, .slice() is needed
Out of bound indices will not be tracked by MobX ... ListView , SectionList also regards ObservableArray as unsuitable prop.
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 Hashnode Post
No results found
Top GitHub Comments
@coolguy001tv I ran into this same issue and @brentvatne is correct, you need to
slice()
your nested arrays to get plain arrays instead of observable arrays. Sothis.listData[0].data
is not compatible withSection.data
, whereasthis.listData[0].data.slice()
will work. See mobx best practices.@CodeXtinction you may not do it right. Here’s my example (pseudo code, just for instruction):
If you can’t understand , you can try pasting your code.