I'm trying to make my component re-render and fetch new data when the props is changed but I can't make it work.
export default class InfoTagEditor extends React.PureComponent<Props, State> {
componentDidUpdate() {
$.get('/webapi/getData?componentId=' + this.props.id, function (data) {
this.setState({value: data});
this.initComponentsDropdownlist();
}.bind(this));
}
}
But this doesn't update the state at all...
Does anyone know what's wrong?
Kendo Editor in Render function:
Render(){
return <Editor
name={"Editor"}
value={this.state.value}
change={this.onChangeEditor}
resizable={true}
/>
}
What worked for me was:
shouldComponentUpdate(nextProps)
{
if (this.props.Id !== nextProps.Id) {
$.get('/webapi/GetData?Id=' + nextProps.Id, function (data) {
this.setState({value: data});
$('#editor').data('kendoEditor').value(this.state.value)
return true;
}.bind(this));
}
if (this.props.name !== nextProps.name) {
return true;
}
return false;
}
But would this be the correct way of doing this? The Id and Name is corresponding, which means that everytime there is a new ID, there will also be a new Name.
Should I do this separate
this.setState({ value: data });
$('#editor').data('kendoEditor').value(this.state.value)
or
this.setState({ value: data },
$('#editor').data('kendoEditor').value(this.state.value));
Managed to get this work:
componentDidUpdate(prevProps) {
if(this.props.id!== prevProps.id) {
$.get('/webapi/GetData?id=' + this.props.id, function (data) {
this.setState({ editorValue: data }, $('#editor').data('kendoEditor').value(data));
}.bind(this));
}
}
Is this a correct way of doing the callback? and does it looks fine? :) or should I move the $('#editor').data('kendoEditor').value(data) outside the setState?