Skip to content

Instantly share code, notes, and snippets.

@fairkmitl
Created October 11, 2018 06:29
Show Gist options
  • Select an option

  • Save fairkmitl/5f8390eccfbb40a124b40ccceb0ef5ac to your computer and use it in GitHub Desktop.

Select an option

Save fairkmitl/5f8390eccfbb40a124b40ccceb0ef5ac to your computer and use it in GitHub Desktop.

Revisions

  1. fairkmitl created this gist Oct 11, 2018.
    2 changes: 2 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@

    <div id="app"></app>
    7 changes: 7 additions & 0 deletions react-fetch-example.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    React fetch example
    -------------------


    A [Pen](https://codepen.io/fair-weerapat/pen/JmNegJ) by [Fair Weerapat](https://codepen.io/fair-weerapat) on [CodePen](https://codepen.io).

    [License](https://codepen.io/fair-weerapat/pen/JmNegJ/license).
    90 changes: 90 additions & 0 deletions script.babel
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    class App extends React.Component {

    constructor(props){
    super(props);
    this.state = {
    repos: []
    };
    }

    handleSearch = (user) =>{
    let url = 'https://api.github.com/users/'+user+'/repos';
    fetch(url).
    then(response => response.json()).then((repos) => {
    console.log(repos);
    console.log(repos.length);
    this.setState({
    repos: repos
    });
    });
    };

    render(){
    return (
    <div className="app-container">
    <h3>React fetch example</h3>
    <SearchBar handleSubmit={this.handleSearch} />
    <RepoList repos={this.state.repos}/>
    </div>
    )
    }
    }

    class SearchBar extends React.Component {
    constructor(props) {
    super(props);
    }

    handleSubmit = (event) => {
    event.preventDefault();
    const text = event.target.text.value;
    this.props.handleSubmit(text);
    };

    render() {
    return (
    <form onSubmit={this.handleSubmit}>
    <input
    name="text"
    className="form-control"
    type="text"
    placeholder="Type github user and press ENTER"
    />
    </form>
    );
    }
    }


    class RepoList extends React.Component {

    render(){
    var rows = [];
    this.props.repos.map((repo,index) => rows.push(<RepoItem key={index} repo={repo} />))
    return (
    <div className="list-group">
    {rows}
    </div>
    )
    }
    }
    RepoList.defaultProps = {
    repos: []
    };

    class RepoItem extends React.Component {
    render(){
    return (
    <a href="#" className="list-group-item list-group-item-action flex-column align-items-start">
    <div className="d-flex w-100 justify-content-between">
    <h5 className="mb-1">{this.props.repo.name}</h5>
    <small>{new Date(Date.parse(this.props.repo.created_at)).toLocaleDateString()}</small>
    </div>
    <p className="mb-1">{this.props.repo.description}</p>
    <small>{this.props.repo.html_url}</small>
    </a>
    )
    }
    }

    ReactDOM.render(<App/>,document.getElementById('app'));
    4 changes: 4 additions & 0 deletions scripts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
    16 changes: 16 additions & 0 deletions style.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    body {
    background: #538cb5;
    }
    .app-container {
    background: #fff;
    max-width: 480px;
    margin: 60px auto;
    padding: 40px 25px;
    }

    h3 {
    color: #538cb5;
    }
    input {
    margin: 20px 0;
    }
    1 change: 1 addition & 0 deletions styles
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />