实际上,在调用 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
页面被重新渲染
有没有更好的解决方案来避免重新渲染?