【发布时间】:2019-02-13 10:11:15
【问题描述】:
我遇到了异步数据问题。我有一个处理从服务器加载的数据的组件
import React, {Component} from 'react';
import {Col, Row} from 'antd';
import {persiststore} from "../../../../store";
class Step2Question extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
const questions = [];
if (this.props.questions.length === 0) {
this.props.callApi('GET', '/api/products/' + this.props.selectedProduct, null, this.props.token, (data) => {
// this.setState({ranks: data.data});
data.data.productQuestionModels.forEach(ele => {
this.props.callApi('GET', '/api/questions/' + ele.questionId, null, this.props.token, (data) => {
questions.push(data.data);
})
})
}, ...[, , ,], () => {
this.props.addQuestions(questions);
persiststore.flush().then(() => {
this.props.disableNext(false);
})
persiststore.persist();
}, 'questions');
}
}
render() {
return (
<Row>
{this.props.questions.map((ele, idx) => (
<Col key={idx} style={{paddingTop: '30px'}} md={12} xs={12} sm={12}>
<strong>Question {idx + 1}: {ele.content}</strong>
</Col>))}
</Row>
);
}
}
我不知道如何从服务器渲染加载问题。如果我在连接方法中使用选项{pure: false},它将呈现一些项目而不是全部。请帮助我,非常感谢
【问题讨论】:
-
尝试使用 axios (npmjs.com/package/axios) 发出请求并寻找 async/await
-
我已经用过 axios 了。这是我通过中间件使用的动作
标签: javascript reactjs redux react-redux redux-persist