简体   繁体   English

数据未从 API [ReactJs] 加载到列表中

[英]data not loading onto list from API [ReactJs]

so I am currently working on an admin control panel that retrieves objects from an API.所以我目前正在开发一个从 API 检索对象的管理控制面板。

The API has two different endpoints, it's either of: API 有两个不同的端点,它是:

http://localhost:3000/videos
http://localhost:3000/manuals

And the API's both return objects with sets of data such as "id, url, title and thumbnailUrl" API 都返回带有数据集的对象,例如“id、url、title 和 thumbnailUrl”

I have a module that's responsible for the card that has the following code:我有一个模块负责具有以下代码的卡:

export interface Manual {
  id?: string | number;
  url: string;
  title: string;
  thumbnail?: string | null;
}

I have a module that's responsible for creating the actual HelpCard components And then the HelpCard components are then loaded up into my HelpList component And then finally, I have my HelpAdmin.view module that brings it all together我有一个模块负责创建实际的 HelpCard 组件然后将 HelpCard 组件加载到我的 HelpList 组件中最后,我有我的 HelpAdmin.view 模块,将它们组合在一起

The issue is, even when I call a问题是,即使我打电话给

console.log(this.props.data);

within my HelpList module, I am returned with 8 empty arrays that look like so:在我的 HelpList 模块中,我返回了 8 个空的 arrays,如下所示:

[]
length: 0
__proto__: Array(0)

I am curious as to why my video / manual cards are not actually being displayed onto my list?我很好奇为什么我的视频/手册卡实际上没有显示在我的列表中?

Thank you in advance for reading and helping.提前感谢您的阅读和帮助。

You are calling the API in HelpList Component and setting it to state In your HelpAdminView component you have a state of manuals that is an empty array and passing it as a data prop, it would remain empty as you are not updating it,您在 HelpList 组件中调用 API 并将其设置为 state 在您的 HelpAdminView 组件中,您有一个 state,手册是一个空数组,因为您不会将其作为空数组传递,因此您不会将其传递为空数组

You can do your API calling in HelpAdminView component and then pass it as a prop to your HelpList component您可以在 HelpAdminView 组件中调用 API,然后将其作为道具传递给您的 HelpList 组件

Hope it helps希望能帮助到你

The manuals and videos data is in your HelpList component's state, but you are using this.props.data.map which comes empty from your HelpListAdmin using data={this.state.videos}.手册和视频数据在您的 HelpList 组件的 state 中,但您使用的是 this.props.data.map,它使用 data={this.Z9ED39E2EA931586B6A985A6942 从您的 HelpListAdmin 中为空。

So you need replace the related code like this in your HelpList component's render.因此,您需要在 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}
                />
              ))}

But if you want to manage the state in your HelpAdminView component, you need to make the api calls in this component instead of HelpList component.但是,如果您想在 HelpAdminView 组件中管理 state,则需要在此组件中而不是 HelpList 组件中调用 api。

And I think you should keep the state in parent (HelpAdminView) and pass the manuals and videos to the HelpList component.而且我认为您应该将 state 保留在父级(HelpAdminView)中,并将手册和视频传递给 HelpList 组件。

So you need to move this code from HelpList to HelpAdminView.因此,您需要将此代码从 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);
      });
  };

Remove the state in HelpList component.删除 HelpList 组件中的 state。

And pass the videos and manuals as props (like you did now) to HelpList component.并将视频和手册作为道具(就像你现在所做的那样)传递给 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>

And use this props in HelpList component (as you did now).并在 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>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM