I'm using React and fetch() to develop an UI and I ended up doing this:
getOperatorsList: function ( obj ) {
fetch( 'http://x.x.x.x/operators.list',
{
method: 'GET',
credentials: 'include'
}
).then( function ( response ) {
return response.json()
} ).then( function ( json ) {
if ( json.statusCode === 3 ) {
cookieService.unsetCookie( 'sessId' );
}
obj.setState( { data: json }, () => obj.forceUpdate() );
} ).catch( function ( ex ) {
console.log( 'parsing failed', ex );
} )
}
This is called in my component Operators that looks like this
var Operators = React.createClass( {
getInitialState: function () {
return {
data: [{ "data": "Loading" }]
}
},
componentDidMount: function () {
operatorsService.getOperatorsList( this );
},
render: function () {
return (
<div>
<Row >
<Col>
<DataTablesCustom data={this.state.data} />
</Col>
</Row>
</div>
);
}
});
I already had a look at this question, and the code doesn't work for me.
This works fine but do I really need to use forceUpdate() or do I have a way to make the code "cleaner" ?
EDIT: there was a setState that looked like this this.setState({stuff: stuff}, this.function()}); in a child component. I was able to remove forceUpdate() after changing the setState to this.setState({stuff: stuff}, () => this.function()});.
forceUpdateat all if he restructure his code a bit better.