【问题标题】:React GitHub Repo API反应 GitHub 回购 API
【发布时间】:2020-04-07 03:51:36
【问题描述】:

我正在做一些事情,允许用户输入用户名并在 repolist 组件内的单独反应组件中显示该用户的所有 github 存储库。

我可以获取所有 repos 的 JSON 数据,但我无法遍历对象以显示每个 repo 的卡片,我不知道如何引用嵌套在对象内的数据。

我应该注意到 testData 最初填充了测试数据,我只能将其作为一维数组而不是对象来工作。

App.js:


    import React from 'react';
    import { CardList } from './Components/CardList.js';
    import { testData } from './Components/Card.js'
    import { Form } from './Components/Form.js'; 
    import './App.css';

    class App extends React.Component {

      state = {
        profiles: testData,
      };
      addNewSearch = (searchData) => {
        this.setState(prevState => ({
          profiles: [...prevState.profiles, searchData],
        }))
      };

      render() {
        return (
          <div className="App">
            <header className="App-header">
            <div>{this.props.title}</div>
              <div className=" search-box" style={{textAlign: 'center'}}>
                <Form onSubmit={this.addNewSearch}/>
              </div>
              <div className="bgbox rounded">

                <div className="cardbox">
                  <CardList profiles={this.state.profiles} />
                </div>
              </div>
            </header>
          </div>
        );
      }
    }

    export default App

Form.js


    import React from 'react';
    import axios from 'axios';

    export class Form extends React.Component {
        state = { userName: '' };
        handleSubmit = async (event) => {
            event.preventDefault();
            const resp = await
            axios.get(`https://api.github.com/users/${this.state.userName}/repos?per_page=250`)
            this.props.onSubmit(
                resp.data
            );

        };
        render() {
            return (
                <form onSubmit={this.handleSubmit}>
                    <div className="btn-group">
                        <input type="text" value={this.state.userName} className="form-control" placeholder="GitHub User" onChange={event => this.setState({userName: event.target.value })}></input>
                        <button className="btn btn-dark">Go!</button>
                    </div>
                </form>
            )
        }
    }

卡片.js


    import React from 'react';

    export const testData = [];


    export class Card extends React.Component {
        render() {
            const repo = this.props;
            return (
                <div className="card border-secondary mb-3" style={{ width: '20rem', fontSize: '12px'}}>
                    <h5 className="card-header">{repo.name}</h5>
                    <div className="card-body text-secondary">
                        <p className="card-text">{repo.name}</p>
                    </div>
                </div>
            );
        }
    }


CardList.js


    import React from 'react';
    import { Card } from './Card.js'

    export const CardList = (props) => (
        <div>
            {props.profiles.map(profile => <Card key={profile.id} {...profile} />)}
        </div>
    );

编辑:当前外观的屏幕截图

【问题讨论】:

    标签: javascript json reactjs api axios


    【解决方案1】:

    首先,您需要查看您提出的请求的结果。

    [
      {
        "id": 123456,
        "name": "bla-bla-repo",
        ...
      },
      ...
    ]
    

    对于每个请求,您都会获得一个用户的存储库数组。

    您正在存储某个用户的所有存储库,因此您有一个用户存储库数组。 (数组数组)

    在执行profiles.map(profile =&gt; ...) 时,profile 是一个 repos 数组。你无法访问profile.id

    .map 在另一个 .map 中可以做什么。

    {props.profiles.map(profile => profile.map(repo => <Card key={repo.id} repo={repo} />))}
    

    这将列出所有用户的所有 repos。


    如果这不是您想要做的,并且您只想显示一个用户的所有存储库,您需要传递给CardList,只有一个配置文件。

    <CardList profiles={this.state.profiles[indexOfTheProfileYouWantToDisplay]} />
    

    【讨论】:

    • 嗨,这是完美的。我将您的最后一行添加到我的 App.js 中并对其进行了更改,以便它只呈现一个卡片列表。我添加了一行虚拟数据,因此它会显示一些内容,否则会出错。当我搜索用户名时,它用 repos 列表替换了虚拟数据!请问,你知道如何在数组中没有[0]的情况下不抛出错误吗?
    • @HollowA 你应该做一些检查,比如this.state.profiles &amp;&amp; this.state.profiles[0],因为profiles 可以是nullundefined。您应该在每次使用 .map 时进行此检查,也在 {props.profiles &amp;&amp; props.profiles.map(...) 中进行检查
    猜你喜欢
    • 1970-01-01
    • 2013-06-12
    • 2013-02-01
    • 2012-05-28
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2023-03-20
    相关资源
    最近更新 更多