Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react axios Card List

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import Axios from "axios";
// GitHub usernames: gaearon, sophiebits, sebmarkbage, bvaughn

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

class Card extends React.Component {
render() {
const profile = this.props;
return (
<div className="github-profile">
<img src={profile.avatar_url} />
<div className="info">
<div className="name">{profile.name}</div>
<div className="company">{profile.company}</div>
</div>
</div>
);
}
}

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}`);
this.props.onSubmit(resp.data);
this.setState({ userName: '' });
};
render() {
return (
<form onSubmit={this.handleSubmit}>
<input
type="text"
value={this.state.userName}
onChange={event => this.setState({ userName: event.target.value })}
placeholder="GitHub username"
required
/>
<button>Add card</button>
</form>
);
}
}

class App extends React.Component {
state = {
profiles: [],
};
addNewProfile = (profileData) => {
this.setState(prevState => ({
profiles: [...prevState.profiles, profileData],
}));
};
render() {
return (
<div>
<div className="header">{this.props.title}</div>
<Form onSubmit={this.addNewProfile} />
<CardList profiles={this.state.profiles} />
</div>
);
}
}

export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: supertest formdata 
Javascript :: how calculate number of digits of number 
Javascript :: datatables buttons do not appear localisation 
Javascript :: getx oninit 
Javascript :: only allow requests from domain express 
Javascript :: calculate age given the birth date in the format yyyymmdd 
Javascript :: api streamelements watchtime 
Javascript :: react loop return 
Javascript :: if in javascript 
Javascript :: yup oneof 
Javascript :: nodejs curd insert update delete 
Javascript :: simultaneos mouse buttons clicked js 
Javascript :: html2canvas not getting image if file field src is url 
Javascript :: nodejs export all mongodb collections 
Javascript :: foreach loop 
Javascript :: loop in javascript 
Javascript :: hostlistner 
Javascript :: Flutter list of JSONs to Objects 
Javascript :: comment faire pour écrire un texte en javascript 
Javascript :: reactjs wait for image to load from url 
Javascript :: react axios fetchData with loading screen plus API 
Javascript :: div diseaper going down 
Javascript :: copying table element to clipboard using javascript 
Javascript :: emergency food meme 
Javascript :: export to csv - Javascript - Download CSV as File 
Javascript :: loop through async javascript -1 
Javascript :: final-form reset form 
Javascript :: vue js data bind 
Javascript :: ios safari controls cover element 
Javascript :: vue router Async Scrolling 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =