fetch 关注分离设计思想
import React, { Component } from 'react'
export default class Search extends Component {
search = () => {
const id = 1;
fetch(`/api/search/getdata?id=${{id}`)
.then(
response =>{
return response.json()
},
error => {
return new Promise(()=>{})
console.log(error)
}
)
.then(
response =>{
console.log(response)
},
error => {
console.log(error)
}
)
}
search = async ()=> {
try {
const id = 1;
const res = await fetch(`/api/search/getdata?id=${{id}`)
const data = await res.json()
} catch (erro) {
conslose.log(erro)
}
}
render() {
return (
<section className="jumbotron">
<div>
<button onClick={this.search}>搜索</button>
</div>
</section>
)
}
}