import React from 'react'; import ReactDOM from 'react-dom'; function fnFetch(url){ return fetch(url).then(res=>{ if(res.ok){ return res.json(); } }).then(res=>{ return res; }) } class App extends React.Component{ state ={ city:'', data:{} }; fetchData = async () =>{ let city = this.state.city; let {result} = await fnFetch(`https://bird.ioliu.cn/weather?city=${city}`); //console.log(result); this.setState({ data:result }); }; keySend = (ev) =>{ if(ev.which === 13){ this.fetchData(); } } handleChange =(ev) =>{ let city = ev.target.value; this.setState({city}); } render(){ let {data} = this.state; return (
城市名:

{data.templow} - {data.temphigh}

) } } ReactDOM.render( , document.getElementById('root'));