import request from 'request';
var pdfAnnotations;
request('GET', url_ws).done((res) => {
if (res.statusCode==200) {
parseString(res.getBody(),{explicitArray:false}, function (err, result) {
pdfAnnotations=result['root']['element'];
console.log(pdfAnnotations);// it's OK values
});
}
});
console.log(pdfAnnotations);//display "undefined"//first
class App extends Component<Props, State> {
state = {
info: pdfAnnotations//is null
};
state: State;
我需要从web api数据加载状态,状态是'未定义&#39;,是可能同步还是其他方式???
答案 0 :(得分:0)
您可以将信息的初始值设置为null
,然后从服务器加载数据并调用this.setState
以设置componentDidMount
import request from 'request';
class App extends React.Component {
constructor(props) {
this.state = {
info: null
}
}
componentDidMount() {
request('GET', url_ws).done((res) => {
if (res.statusCode == 200) {
parseString(res.getBody(), { explicitArray: false }, (err, result) => {
const pdfAnnotations = result['root']['element'];
this.setState({ info: pdfAnnotations });
});
}
});
}
}