1Component with Data
2class App extends React.Component {
3 constructor(props) {
4 super(props);
5 this.state = {
6 items: [],
7 isLoaded: false,
8 };
9 }
10
11 componentDidMount() {
12 fetch('https://jsonplaceholder.typicode.com/posts')
13 .then(res => res.json())
14 .then(result => {
15 this.setState({
16 isLoaded: true,
17 items: result
18 });
19 });
20 }
21
22 render() {
23 const { items } = this.state;
24 if (!isLoaded) {
25 return <div>Loading ... </div>;
26 } else {
27 return (
28 <ul>
29 {items.map(item => (
30 <li key={item.id}>
31 <h3>{item.title}</h3>
32 <p>{item.body}</p>
33 </li>
34 ))}
35 </ul>
36 );
37 }
38 }
39}
40Fetching Data - Ha
1/* React get method. */
2
3componentWillMount(){
4 fetch('/getcurrencylist',
5 {
6 /*
7 headers: {
8 'Content-Type': 'application/json',
9 'Accept':'application/json'
10 },
11 */
12 method: "get",
13 dataType: 'json',
14 })
15 .then((res) => res.json())
16 .then((data) => {
17 var currencyList = [];
18 for(var i=0; i< data.length; i++){
19 var currency = data[i];
20 currencyList.push(currency);
21 }
22 console.log(currencyList);
23 this.setState({currencyList})
24 console.log(this.state.currencyList);
25 })
26 .catch(err => console.log(err))
27 }
1import React, { Component } from 'react';
2class App extends Component {
3 constructor(props) {
4 super(props);
5 this.state = {
6 data: null,
7 };
8 }
9 componentDidMount() {
10 fetch('https://api.mydomain.com')
11 .then(response => response.json())
12 .then(data => this.setState({ data }));
13 }
14 ...
15}
16export default App;
17