I'm just a beguinner in React-Native and i'm getting crazy every day with somo unexpected behaviors. Of course it is my fault, I'm still learning. Today i'm struggling to understand whats is goin on with my code.
I have a function that call an array of objects on my local storage. If I console the response from the promise it gives me a perfect array (i think). After that I setState and agin it appears correctly on my console. But, for my surprise, my list component doesn't render anything (and it's surely working as it was before).
I'll put my code attached:
/*FUNCTION FILE*/
getAllData: async () => {
var data = [{id:null, address:null, name:null}];
AsyncStorage.getAllKeys((err, keys) => {
AsyncStorage.multiGet(keys, (err, stores) => {
stores.map((result, i, store) => {
// get at each store's key/value so you can work with it
var key = store[i][0];
var value = store[i][1];
data[i] = {id: i, address: key, name: value}
});
});
});
return data},
//MAIN FILE (DAD COMPONENT)
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import NewTagButton from '../components/NewTagButton';
import TagList from '../components/TagList';
import storage from '../functions/Storage';
import { Object } from 'core-js';
//import { TagPosition } from '../functions/GPSTag'
class TagsScreen extends Component {
constructor(props) {
super(props);
this.state = {message:"",
pairedDevices: [],
};
}
componentDidMount () {
var self = this;
storage.getAllData() //tem que esperar a promise
.then(function (devices) {
self.setState({message: "Scaneamento finalizado."});
self.setState({pairedDevices: devices});
console.log("devices")
console.log(devices)
console.log("State")
console.log(self.state.pairedDevices)
})
.catch(()=> console.log("Promise rejected"))
}
render() {
return (
<View>
<Text>{this.state.message}</Text>
<TagList devices = {this.state.pairedDevices} origem = 'MyTag'/>
<NewTagButton/>
</View>
);
}
}
export default TagsScreen;
LIST COMPONENT THAT SHOULD RENDER THE LIST
import React from 'react';
import { View, StyleSheet } from 'react-native';
import TagItem from '../components/TagItem'
const TagList = props => {
const devicesList = props.devices;
const origem = props.origem;
//aqui a gente vai passar device a device para a page TagItem
const items = devicesList.map((device) =>
<TagItem
key = {device.address}
address = {device.address}
name = {device.name}
origem = {origem}/>
);
return (
<View key = {items} styles = {styles.container}>
{items}
</View>
);
}
const styles = StyleSheet.create({
container : {
alignItems:'center',
}
})
export default TagList;
Thank you.
EDIT: I've put some logs between the components, the image is on a url because I don't have enough reputation haha
EDIT2:
I just created a silly button to set a state just with a string. After this state changing, the app render the list. For sure is a promise problem, can someone help me? =]