Dam*_*phe 5 javascript api reactjs
实际上,在调用 API 时,我在 React 中遇到了一个小问题,因为ComponentWillMount已弃用。
我这样做了:
class MyClass extends Component {
constructor() {
super();
this.state = {
questionsAnswers: [[{ answers: [{ text: '', id: 0 }], title: '', id: 0 }]],
},
};
}
componentDidMount() {
this.getQuestions();
}
getQuestions = async () => {
let questionsAnswers = await Subscription.getQuestionsAndAnswers();
questionsAnswers = questionsAnswers.data;
this.setState({ questionsAnswers });
};
Run Code Online (Sandbox Code Playgroud)
因此,页面第一次渲染时没有questionsAsnwers,当我得到页面时,questionAnswers页面被重新渲染
有没有更好的解决方案来避免重新渲染?
componentDidMount根据react文档,处理API调用的最佳方法是在react lifeCycle方法中。此时您所能做的就是添加一个微调器以使您的组件更加用户友好。
希望在下一个 React 版本中。React 将引入一种新方法来解决此类问题,使用suspense方法https://medium.com/@baphemot/understanding-react-suspense-1c73b4b0b1e6
| 归档时间: |
|
| 查看次数: |
11972 次 |
| 最近记录: |