第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

React/axios 在渲染前獲取數(shù)據(jù)

React/axios 在渲染前獲取數(shù)據(jù)

牛魔王的故事 2023-03-10 16:43:57
我正在開(kāi)發(fā)一個(gè)反應(yīng)應(yīng)用程序,我正在嘗試在渲染之前獲取數(shù)據(jù)。我在 NET 上嘗試了很多可用的解決方案,但似乎沒(méi)有任何效果。我正在嘗試從我的 nodeJS 服務(wù)器獲取數(shù)據(jù)(服務(wù)文件夾中的文件,以便我可以管理它們、添加它們等)import React, {Component} from 'react';import Checkbox from './Checkbox';import axios from 'axios';const OPTIONS = [];console.log('dd');class App extends Component {  state = {    checkboxes: OPTIONS.reduce(      (options, option) => ({        ...options,        [option]: false,      }),      {}    ),    load: true,  };  componentWillMount() {    this.callF();    this.setState({load: false});  }  selectAllCheckboxes = (isSelected) => {    Object.keys(this.state.checkboxes).forEach((checkbox) => {      this.setState((prevState) => ({        checkboxes: {          ...prevState.checkboxes,          [checkbox]: isSelected,        },      }));    });  };  callF = () => {    console.log('Yes');    axios      .get('http://localhost:3007/service')      .then(function (response) {        response.data.forEach((element) => {          if (OPTIONS.indexOf(element) === -1 && element !== 'Master.js' && element !== 'command.txt') {            OPTIONS.push(element);          }        });        console.log('----' + OPTIONS);      })      .catch(function (error) {        console.log('{' + error + '}');      });  };  selectAll = () => {    this.selectAllCheckboxes(true);    console.log('HEY');  };  deselectAll = () => this.selectAllCheckboxes(false);  handleCheckboxChange = (changeEvent) => {    const {name} = changeEvent.target;    this.setState((prevState) => ({      checkboxes: {        ...prevState.checkboxes,        [name]: !prevState.checkboxes[name],      },    }));  };  handleFormSubmit = (formSubmitEvent) => {    formSubmitEvent.preventDefault();    Object.keys(this.state.checkboxes)      .filter((checkbox) => this.state.checkboxes[checkbox])      .forEach((checkbox) => {        console.log(checkbox, 'is selected.');      });  };我嘗試調(diào)用道具,使用componentWillMountandComponentDidMount但我猜我不擅長(zhǎng)這個(gè)。
查看完整描述

1 回答

?
交互式愛(ài)情

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊

既然你提到你需要在渲染之前獲取數(shù)據(jù),我猜你正在獲取數(shù)據(jù),但是在渲染之后。在這種情況下,請(qǐng)考慮條件渲染。例如,添加一個(gè)將保持加載狀態(tài)的新?tīng)顟B(tài)。最初將其設(shè)置為 true。當(dāng)你取東西時(shí) - 將 loading 設(shè)置為 false。根據(jù)加載狀態(tài)(當(dāng)它為 false 時(shí)),您可以呈現(xiàn)所需的一切。

編輯:你在獲取時(shí)沒(méi)有更新你的加載狀態(tài) - 請(qǐng)刪除引號(hào):你有 -this.setState({'load': false}); 試試this.setState({load: false});


查看完整回答
反對(duì) 回復(fù) 2023-03-10
  • 1 回答
  • 0 關(guān)注
  • 282 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)