亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React/axios 在渲染前獲取數據

React/axios 在渲染前獲取數據

牛魔王的故事 2023-03-10 16:43:57
我正在開發一個反應應用程序,我正在嘗試在渲染之前獲取數據。我在 NET 上嘗試了很多可用的解決方案,但似乎沒有任何效果。我正在嘗試從我的 nodeJS 服務器獲取數據(服務文件夾中的文件,以便我可以管理它們、添加它們等)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.');      });  };我嘗試調用道具,使用componentWillMountandComponentDidMount但我猜我不擅長這個。
查看完整描述

1 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

既然你提到你需要在渲染之前獲取數據,我猜你正在獲取數據,但是在渲染之后。在這種情況下,請考慮條件渲染。例如,添加一個將保持加載狀態的新狀態。最初將其設置為 true。當你取東西時 - 將 loading 設置為 false。根據加載狀態(當它為 false 時),您可以呈現所需的一切。

編輯:你在獲取時沒有更新你的加載狀態 - 請刪除引號:你有 -this.setState({'load': false}); 試試this.setState({load: false});


查看完整回答
反對 回復 2023-03-10
  • 1 回答
  • 0 關注
  • 290 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號