Simplified problem. Calling this.setState inside a Promise displays the pending Promise to the end.
My problems:
- This .setState does not return immediately
- I expected it to be asynchronous, so the pending promise would be closed first.
- If something breaks inside the render function, catch is called inside Promise.
- Perhaps the same problem as 1) that it seems that the render is still in the context of the promise in which this.setState was called.
import dummydata_rankrequests from "../dummydata/rankrequests";
class RankRequestList extends Component {
constructor(props) {
super(props);
this.state = { loading: false, data: [], error: null };
this.makeRankRequestCall = this.makeRankRequestCall.bind(this);
this.renderItem = this.renderItem.bind(this);
}
componentDidMount() {
this.makeRankRequestCall()
.then(done => {
console.log("done");
});
}
makeRankRequestCall() {
console.log('call makeRankRequestCall');
try {
return new Promise((resolve, reject) => {
resolve(dummydata_rankrequests);
})
.then(rankrequests => {
console.log('START makeRankRequestCall-rankrequests', rankrequests);
this.setState({ data: rankrequests.data, loading: false });
console.log('END _makeRankRequestCall-rankrequests');
return null;
})
.catch(error => {
console.log('_makeRankRequestCall-promisecatch', error);
this.setState({ error: RRError.getRRError(error), loading: false });
});
} catch (error) {
console.log('_makeRankRequestCall-catch', error);
this.setState({ error: RRError.getRRError(error), loading: false });
}
}
renderItem(data) {
const height = 200;
return (
<View style={[styles.item, {height: height}]}>
</View>
);
}
render() {
let data = [];
if (this.state.data && this.state.data.length > 0) {
data = this.state.data.map(rr => {
return Object.assign({}, rr);
});
}
console.log('render-data', data);
return (
<View style={styles.container}>
<FlatList style={styles.listContainer1}
data={data}
renderItem={this.renderItem}
/>
</View>
);
}
}
Currrent magazines show:
- rendering data, []
- START makeRankRequestCall-rankrequests
- rendering data, [...]
- _makeRankRequestCall-promisecatch Error: unknown named module ...
- rendering data, [...]
- Possible raw promises
Android Emulator
"": "16.0.0-alpha.12",
"-": "0.46.4",
:
wrapping setTimeout this.setState
setTimeout(() => {
this.setState({ data: respData.data, loading: false });
}, 1000);
EDIT2:
github
https://github.com/facebook/react-native/issues/15214