1import React, { Fragment, useState, useEffect } from 'react';
2import axios from 'axios';
3
4function App() {
5 const [data, setData] = useState({ hits: [] });
6 const [query, setQuery] = useState('redux');
7
8 useEffect(() => {
9 const fetchData = async () => {
10 const result = await axios(
11 'https://hn.algolia.com/api/v1/search?query=redux',
12 );
13
14 setData(result.data);
15 };
16
17 fetchData();
18 }, []);
19
20 return (
21 <Fragment>
22 <input
23 type="text"
24 value={query}
25 onChange={event => setQuery(event.target.value)}
26 />
27 <ul>
28 {data.hits.map(item => (
29 <li key={item.objectID}>
30 <a href={item.url}>{item.title}</a>
31 </li>
32 ))}
33 </ul>
34 </Fragment>
35 );
36}
37
38export default App;
1const users = data.map(u =>
2 <div>
3 <p>{u.id}</p>
4 <p>{u.name}</p>
5 <p>{u.email}</p>
6 <p>{u.website}</p>
7 <p>{u.company.name}
8
9 </div>
10 )
11this.setState({users})
12
13
14import React, {Component} from 'react'
15import axios from '../../axios'
16
17export default class users extends Component {
18 constructor(props) {
19 super(props);
20 this.state = {
21 Users: []
22 };
23 }
24 getUsersData() {
25 axios
26 .get(`/users`, {})
27 .then(res => {
28 const data = res.data
29 console.log(data)
30 const users = data.map(u =>
31 <div>
32 <p>{u.id}</p>
33 <p>{u.name}</p>
34 <p>{u.email}</p>
35 <p>{u.website}</p>
36 <p>{u.company.name}</p>
37 </div>
38 )
39
40 this.setState({
41 users
42 })
43
44 })
45 .catch((error) => {
46 console.log(error)
47 })
48
49 }
50 componentDidMount(){
51 this.getUsersData()
52 }
53 render() {
54
55 return (
56 <div>
57 {this.state.users}
58 </div>
59 )
60 }
61}