Jsem nový mobx. Snažím se přenos dat do FlatList z oblíbených uložit do favoritescreen straně s mobx, ale flatlist se objeví prázdná, proč?
Důvod, proč jsem použít listData() v bookmarkList je, že pokud nechci používat šipky funkce v bookmarkList, mi to dává následující chyba;
[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_this2.userId')]
oblíbené(uložit);
import { observable, computed, action, makeObservable, configure } from "mobx";
import { bookmarkApi, petApi } from '../../constants/apiURL';
import AsyncStorage from '@react-native-async-storage/async-storage';
configure({
enforceActions: "never",
})
class Favorites {
userId = 0;
petList = [];
constructor() {
this.bookMarkList()
makeObservable(this, {
userId: observable,
petList: observable,
bookMarkList: action,
})
}
bookMarkList = async () => {
const value = await AsyncStorage.getItem('userId')
if (value != null) {
this.userId = value;
listData = () => {
bookmarkApi.get('/').then(function (responseJson) {
if (responseJson) {
this.petList = responseJson.data.filter(data => data.userId == this.userId)
}
})
}
}
}
}
export const favoriteStore = new Favorites();
FavoriteScreen.js
import { observer } from 'mobx-react';
import { favoriteStore } from '../store/favorites';
function FavoriteScreen() {
const {userId, petList} = favoriteStore;
const Item = ({ petName }) => (
<View style={styles.item}>
<Text style={styles.title}>{petName}</Text>
</View>
);
const renderItem = ({ item }) => (
<Item petName={item.petName} />
);
return (
<SafeAreaView style={styles.container}>
<FlatList
data={petList}
keyExtractor={item => item.bookmarkId}
renderItem={renderItem}
/>
</SafeAreaView>
)
}
export default observer(FavoriteScreen);