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

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

數據未從 API [ReactJs] 加載到列表中

數據未從 API [ReactJs] 加載到列表中

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

2 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

手冊和視頻數據處于您的 HelpList 組件的狀態,但您正在使用 this.props.data.map,它使用 data={this.state.videos} 從您的 HelpListAdmin 中變為空。


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


              {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 組件中管理狀態,你需要在這個組件而不是 HelpList 組件中進行 api 調用。


而且我認為您應該將狀態保留在父級 (HelpAdminView) 中,并將手冊和視頻傳遞給 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 組件中的狀態。


并將視頻和手冊作為道具(就像您現在所做的那樣)傳遞給 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 組件中使用這個道具(就像你現在所做的那樣)。


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>

    );

  }

}


查看完整回答
反對 回復 2021-12-02
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

您正在 HelpList 組件中調用 API 并將其設置為 state 在您的 HelpAdminView 組件中,您有一個手冊狀態,它是一個空數組并將其作為數據道具傳遞,因為您沒有更新它,它將保持為空,

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

希望能幫助到你


查看完整回答
反對 回復 2021-12-02
  • 2 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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