question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SectionList doesn't work right with MobX

See original GitHub issue

I 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

  1. react-native -v: react-native-cli: 2.0.1 react-native: 0.46.0
  2. node -v: v7.10.0
  3. npm -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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

12reactions
stephenlaughtoncommented, Jul 24, 2017

@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. So this.listData[0].data is not compatible with Section.data, whereas this.listData[0].data.slice() will work. See mobx best practices.

7reactions
coolguy001tvcommented, Jul 26, 2017

@CodeXtinction you may not do it right. Here’s my example (pseudo code, just for instruction):

let listData = [
            {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"
            }]},
        ];


//
@computed get formattedList(){
        return this.listData.map((v)=>{
            return {
                month: v.month,
                data: v.data.slice(),//You need to slice data
            }
        }).slice();// And slice the listData
    }
//
<SectionList
                    ...
                    sections={this.props.BillStore.formattedList}
                />

If you can’t understand , you can try pasting your code.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found