React Lifecycle- 绘图是使用初始状态值绘制的,而不是使用获取的值
React Lifecycle- The plot is drawn using the initial state value, and not using the fetched value
我想从 flask 中获取变量 'r2score' 的值。取值成功。我什至写了一个 console.log(r2score) 语句来查看提取是否有效。这就是问题所在。最初它记录的值为 0.1,这是它的初始状态。然后在控制台的下一行中,它记录了一个值 0.26,这是从 flask 中获取的值。所以至少获取成功了。但是,正在绘制的图是用 0.1(它的初始状态)而不是 0.26(它的获取值)绘制的。
我的代码:
import ReactDOM from "react-dom";
import React from "react";
import { Liquid } from "@antv/g2plot";
import ReactG2Plot from "react-g2plot";
class R2ScorePlot extends React.Component {
constructor(props) {
super(props);
this.state = { r2score: 0.1 };
}
componentDidMount() {
fetch(`/fetch_regressionmodel`)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Something went wrong ...");
}
})
.then(info =>
this.setState({
r2score: info.R2score
})
).then( this.forceUpdate() )
.catch(error => this.setState({ error }));
}
shouldComponentUpdate() {
return true;
}
render() {
const { r2score } = this.state;
console.log(r2score);
const config = {
title: {
visible: false,
text: ""
},
description: {
visible: false,
text: ""
},
min: 0,
max: 1,
value: r2score,
statistic: {
formatter: value => ((1 * value) / 1).toFixed(1)
}
};
return (
<div>
<ReactG2Plot Ctor={Liquid} config={config} />
</div>
);
}
}
export default R2ScorePlot;
Console Image
React Dev Tools
已解决问题。解决方案是将图形组件包装在
<div key={r2score}>...</div>
以便图表在密钥更改时重建。
我的代码:
import ReactDOM from "react-dom";
import React from "react";
import { Liquid } from "@antv/g2plot";
import ReactG2Plot from "react-g2plot";
class R2ScorePlot extends React.Component {
constructor(props) {
super(props);
this.state = { r2score: 0.1 };
}
componentDidMount() {
fetch(`/fetch_regressionmodel`)
.then(response => {
if (response.ok) {
this.setState({ spinloading: false });
return response.json();
} else {
throw new Error("Something went wrong ...");
}
})
.then(info =>
this.setState({
r2score: info.Explained_Variance_score
})
).catch(error => this.setState({ error }));
}
shouldComponentUpdate() {
return true;
}
render() {
const { r2score } = this.state;
console.log(r2score);
const config = {
title: {
visible: false,
text: ""
},
description: {
visible: false,
text: ""
},
min: 0,
max: 1,
value: r2score,
statistic: {
formatter: value => ((1 * value) / 1).toFixed(1)
}
};
return (
<div key={r2score}>
<ReactG2Plot Ctor={Liquid} config={config} />
</div>
);
}
}
export default R2ScorePlot;
我想从 flask 中获取变量 'r2score' 的值。取值成功。我什至写了一个 console.log(r2score) 语句来查看提取是否有效。这就是问题所在。最初它记录的值为 0.1,这是它的初始状态。然后在控制台的下一行中,它记录了一个值 0.26,这是从 flask 中获取的值。所以至少获取成功了。但是,正在绘制的图是用 0.1(它的初始状态)而不是 0.26(它的获取值)绘制的。
我的代码:
import ReactDOM from "react-dom";
import React from "react";
import { Liquid } from "@antv/g2plot";
import ReactG2Plot from "react-g2plot";
class R2ScorePlot extends React.Component {
constructor(props) {
super(props);
this.state = { r2score: 0.1 };
}
componentDidMount() {
fetch(`/fetch_regressionmodel`)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Something went wrong ...");
}
})
.then(info =>
this.setState({
r2score: info.R2score
})
).then( this.forceUpdate() )
.catch(error => this.setState({ error }));
}
shouldComponentUpdate() {
return true;
}
render() {
const { r2score } = this.state;
console.log(r2score);
const config = {
title: {
visible: false,
text: ""
},
description: {
visible: false,
text: ""
},
min: 0,
max: 1,
value: r2score,
statistic: {
formatter: value => ((1 * value) / 1).toFixed(1)
}
};
return (
<div>
<ReactG2Plot Ctor={Liquid} config={config} />
</div>
);
}
}
export default R2ScorePlot;
Console Image
React Dev Tools
已解决问题。解决方案是将图形组件包装在
<div key={r2score}>...</div>
以便图表在密钥更改时重建。
我的代码:
import ReactDOM from "react-dom";
import React from "react";
import { Liquid } from "@antv/g2plot";
import ReactG2Plot from "react-g2plot";
class R2ScorePlot extends React.Component {
constructor(props) {
super(props);
this.state = { r2score: 0.1 };
}
componentDidMount() {
fetch(`/fetch_regressionmodel`)
.then(response => {
if (response.ok) {
this.setState({ spinloading: false });
return response.json();
} else {
throw new Error("Something went wrong ...");
}
})
.then(info =>
this.setState({
r2score: info.Explained_Variance_score
})
).catch(error => this.setState({ error }));
}
shouldComponentUpdate() {
return true;
}
render() {
const { r2score } = this.state;
console.log(r2score);
const config = {
title: {
visible: false,
text: ""
},
description: {
visible: false,
text: ""
},
min: 0,
max: 1,
value: r2score,
statistic: {
formatter: value => ((1 * value) / 1).toFixed(1)
}
};
return (
<div key={r2score}>
<ReactG2Plot Ctor={Liquid} config={config} />
</div>
);
}
}
export default R2ScorePlot;