Hi i have this wierd problem in which i can access property ooPPon state object but can not access property data of state objects. the only difference is how they are populated.
state.ooPPis hardcodedstate.datais set after calling few functions .
i can see value of state.data as array with one element in react native dev tools but why is this.state.data null in render()?
here is the code:
export default class HomeScreen extends Component {
_retrieveData = async key => {
try {
const value = await AsyncStorage.getItem(key);
if (value !== null) {
// We have data!!
console.log("yay....", value);
return value;
}
} catch (error) {
// Error retrieving data
}
};
getAllKeys = async () => {
let keys = [];
try {
keys = await AsyncStorage.getAllKeys();
} catch (e) {
// read key error
}
return keys;
};
constructor(props) {
super(props);
this.state = {
data:[],
kk:'oo',
ooPP:[{'username':'uuuuu'}]
}
this.getAllKeys().then(keys => {
var promises = keys.map(key => {
return this._retrieveData(key)
.then(value => {
console.log(value, "xx");
return JSON.parse(value);
})
.catch(error => {
//
});
});
Promise.all(promises)
.then(results => {
this.state.data=results
})
.catch(e => {
//
});
});
}
renderSeparator = () => {
return (
<View
style={{
height: 1,
backgroundColor: "#CED0CE"
}}
/>
);
};
render = () => {
return (
<View>
<FlatList
data={this.state.data} //-------> this is not accessible
ItemSeparatorComponent={this.renderSeparator}
renderItem={({ item }) => (
<TOTPItem value={item.username} time={(20 / 30) * 100} />
)}
/>
<Text>dd {this.state.kk}</Text>
</View>
);
}
}
setStatelike you're supposed to, React isn't going to re-render the component. You're still changingthis.stateobviously, because React is just JavaScript, but if you insist on ignoring the docs, don't expect React to behave as expected.i am updating state in constructor so setState() is not the correct waybut I see now where the confusion was coming from :)