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

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

數(shù)據(jù)未從 API [ReactJs] 加載到列表中

數(shù)據(jù)未從 API [ReactJs] 加載到列表中

忽然笑 2021-12-02 15:15:14
所以我目前正在開發(fā)一個(gè)從 API 檢索對(duì)象的管理控制面板。API 有兩個(gè)不同的端點(diǎn),它是:http://localhost:3000/videoshttp://localhost:3000/manualsAPI 都返回帶有數(shù)據(jù)集的對(duì)象,例如“id、url、title 和thumbnailUrl”我有一個(gè)模塊負(fù)責(zé)具有以下代碼的卡:export interface Manual {  id?: string | number;  url: string;  title: string;  thumbnail?: string | null;}我有一個(gè)模塊負(fù)責(zé)創(chuàng)建實(shí)際的 HelpCard 組件 然后將 HelpCard 組件加載到我的 HelpList 組件中 最后,我有我的 HelpAdmin.view 模塊,將它們組合在一起問(wèn)題是,即使我打電話給console.log(this.props.data);在我的 HelpList 模塊中,我返回 8 個(gè)空數(shù)組,如下所示:[]length: 0__proto__: Array(0)我很好奇為什么我的視頻/手冊(cè)卡實(shí)際上沒(méi)有顯示在我的列表中?提前感謝您的閱讀和幫助。
查看完整描述

2 回答

?
守候你守候我

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

手冊(cè)和視頻數(shù)據(jù)處于您的 HelpList 組件的狀態(tài),但您正在使用 this.props.data.map,它使用 data={this.state.videos} 從您的 HelpListAdmin 中變?yōu)榭铡?/p>


所以你需要在你的 HelpList 組件的渲染中替換這樣的相關(guān)代碼。


              {this.state.manuals.map((card: Manual) => (

                <HelpCard

                  card={card}

                  key={card.id}

                  deleteProduct={this.props.onDelete}

                  editProduct={this.props.onEdit}

                  type={this.props.type}

                />

              ))}


但是如果你想在你的 HelpAdminView 組件中管理狀態(tài),你需要在這個(gè)組件而不是 HelpList 組件中進(jìn)行 api 調(diào)用。


而且我認(rèn)為您應(yīng)該將狀態(tài)保留在父級(jí) (HelpAdminView) 中,并將手冊(cè)和視頻傳遞給 HelpList 組件。


因此,您需要將此代碼從 HelpList 移至 HelpAdminView。


  async componentDidMount() {

    const res = await axios.get(this.state.url);

    this.setState({ card: res.data });

    this.loadAdminHelpCard("videos");

    this.loadAdminHelpCard("manuals");


    //console.log(res.data);

  }



  loadAdminHelpCard = (type: "videos" | "manuals"): void => {

    const baseApiUrl = `http://localhost:3000`;

    const url = `${baseApiUrl}/${type}`;

    axios

      .get(url)

      .then((res) => {

        this.setState({ [type]: res.data } as any);

      })

      .catch(function(error) {

        // handle error

        console.log(error);

      });

  };

移除 HelpList 組件中的狀態(tài)。


并將視頻和手冊(cè)作為道具(就像您現(xiàn)在所做的那樣)傳遞給 HelpList 組件。


      <div className="listDisplay">

            <div>

              <div style={{ textAlign: "center" }}>

                <h2>Videos</h2>

              </div>

              <HelpList

                data={this.state.videos}

                type="videos"

                onEdit={this.editProduct}

                onDelete={this.deleteProduct}

              />

            </div>

            <div>

              <div style={{ textAlign: "center" }}>

                <h2>Manuals</h2>

              </div>

              <HelpList

                data={this.state.manuals}

                type="manuals"

                onEdit={this.editProduct}

                onDelete={this.deleteProduct}

              />

            </div>

          </div>

并在 HelpList 組件中使用這個(gè)道具(就像你現(xiàn)在所做的那樣)。


export default class HelpList extends Component<Props, State> {


  static props: any;


  render() {

    console.log(this.props.data);


    return (

      <React.Fragment>

        {this.state.card ? (

          <div className="row">

            {this.props.data.map((card: Manual) => (

              <HelpCard

                card={card}

                key={card.id}

                deleteProduct={this.props.onDelete}

                editProduct={this.props.onEdit}

                type={this.props.type}

              />

            ))}

          </div>

        ) : (

          <h1>

            <br></br>Loading Cards...

          </h1>

        )}

      </React.Fragment>

    );

  }

}


查看完整回答
反對(duì) 回復(fù) 2021-12-02
?
慕尼黑8549860

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

您正在 HelpList 組件中調(diào)用 API 并將其設(shè)置為 state 在您的 HelpAdminView 組件中,您有一個(gè)手冊(cè)狀態(tài),它是一個(gè)空數(shù)組并將其作為數(shù)據(jù)道具傳遞,因?yàn)槟鷽](méi)有更新它,它將保持為空,

您可以在 HelpAdminView 組件中調(diào)用 API,然后將其作為道具傳遞給您的 HelpList 組件

希望能幫助到你


查看完整回答
反對(duì) 回復(fù) 2021-12-02
  • 2 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報(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)