***此组件会进行REST API调用并分析承诺值并以表格形式呈现数据。加载(函数)使API调用并将orderType作为输入。 OrderType作为查询参数从导航组件传递到此处未包含的组件。反应 - 单击链接不会导致重新呈现表
class SampleController extends React.Component {
constructor(props){
super(props);
let orderType = this.props.location.query.orderType;
this.load = this.load.bind(this);
this.load(orderType);
this.state = {orderType: orderType, data: null}
}
load(orderType) {
let self = this;
console.log("order type is" + orderType);
let baseURL = base_urls.orderMetricsBaseURL;
console.log("base URL is", baseURL);
let url = baseURL + "/getData";
let response_data = fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
order_type: orderType
})
})
.then((response) => response.json())
.then((responseJson) => {
let order_data = responseJson;
console.log("responseeeeee is: ", order_data);
self.setState({data: order_data});
self.forceUpdate();
})
.catch((error) => {
console.error(error);
});
}
render() {
var content = <DataTable dataList = {this.state.data} />;
return (
content
);
}
}
export { SampleController as default };
去除forceupdate()未与API的时候订单类型上的UI – Suchi
我误解你的问题改变了(尽管你不需要'forceUpdate没有得到触发的问题帮助()'反正)。那么,每次'orderType'改变时你想调用'load()'?在这种情况下,你应该使用'componentDidUpdate'方法并在那里调用你的'load()'。将以示例更新答案。 –
谢谢你的回应。您能否给我提供一个示例,说明如何在上述上下文中使用componentDidUpdate和componentWillUpdate? – Suchi