Been pulling my hair for a while now and can't get the dataSource to be updated. I've looked at other posts but can't see why this doesn't work. I've debugged and can see the proper json being returned, so the data is there.
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); var ReviewList = React.createClass({ _getMovieReviews: function() { var _urlForReview = this.props.config.api_url + 'movies/' + this.props.movie.id + '/reviews.json?apikey=' + this.props.config.api_key; this.setState(function(){ dataSource: ds.cloneWithRows(['before fetch']); }); fetch(_urlForReview) .then(function(res) { return res.json(); }).then(function (json){ console.log(json); this.setState(function(){ dataSource: ds.cloneWithRows(['fetch done']); }); }); }, getInitialState: function() { return { dataSource: ds.cloneWithRows(['initial']) }; }, componentDidMount: function() { this._getMovieReviews(); }, render: function() { return ( {rowData}} /> ); } });
For now I'm even just trying to update the datasource to anything else than the original value. What am I missing here?
update
Neither "before fetch" or "fetch done" are shown. Only "initial". I'm suspecting that it has to do with that there are other components in the class. Not sure if that could be the case.
update I inserted the sample code one of the answers provided and the sample works in the same view. Super weird. Probably something very stupid I'm missing here...
update
It seems the problem lies with updating the datasource from within the fetch
statement.
setState
that will force a rerender? Also, you should keep therender()
function pure, as stated in the docs, and instead use thecomponentDidMount()
function. – Daniel B