【全栈React】第16天: 显示远程数据
我们的前端应用与我们在其中显示的数据一样有趣。今天,我们开始提出数据请求,并将其集成到我们的应用中。 截至今天,我们已经通过承诺,使用 获取数据我们安装在 第14 天。 为了简单起见,让我们从昨天从 api 服务器获取当前时间的示例中进行演示: 此演示反应组件对 api 服务器发出请求,并从它的时钟中报告当前时间。在我们添加调用来获取之前,让我们创建一些有状态的组件,我们将用来显示时间并更新时间请求。
首先,将显示和获取当前时间的包装组件的基础如下所示。让我们复制并粘贴到我们的应用程序在 import React from 'react'; import 'whatwg-fetch'; import './App.css'; import TimeForm from './TimeForm'; class App extends React.Component { constructor(props) { super(props); this.state = { currentTime: null,msg: 'now' } } // methods we'll fill in shortly fetchCurrentTime() {} getApiUrl() {} handleFormSubmit(evt) {} handleChange(newState) {} render() { const {currentTime,tz} = this.state; const apiUrl = this.getApiUrl(); return ( <div> {!currentTime && <button onClick={this.fetchCurrentTime.bind(this)}> Get the current time </button>} {currentTime && <div>The current time is: {currentTime}</div>} <TimeForm onFormSubmit={this.handleFormSubmit.bind(this)} onFormChange={this.handleChange.bind(this)} tz={tz} msg={'now'} /> <p>We'll be making a request from: <code>{apiUrl}</code></p> </div> ) } } export default App; 前面的组件是我们创建的基本状态响应组件。因为我们要显示一个表单,我们已经包括了 让我们在我们的反应应用程序中使用 touch src/TimeForm.js 现在,让我们添加内容。我们希望我们的 import React from 'react' const timezones = ['PST','MST','MDT','EST','UTC'] export class TimeForm extends React.Component { constructor(props) { super(props); const {tz,msg} = this.props; this.state = {tz,msg}; } _handleChange(evt) { typeof this.props.onFormChange === 'function' && this.props.onFormChange(this.state); } _changeTimezone(evt) { const tz = evt.target.value; this.setState({tz},this._handleChange); } _changeMsg(evt) { const msg = encodeURIComponent(evt.target.value).replace(/%20/,'+'); this.setState({msg},this._handleChange); } _handleFormSubmit(evt) { evt.preventDefault(); typeof this.props.onFormSubmit === 'function' && this.props.onFormSubmit(this.state); } render() { const {tz} = this.state; return ( <form onSubmit={this._handleFormSubmit.bind(this)}> <select onChange={this._changeTimezone.bind(this)} defaultValue={tz}> {timezones.map(t => { return (<option key={t} value={t}>{t}</option>) })} </select> <input type="text" placeholder="A chronic string message (such as 7 hours from now)" onChange={this._changeMsg.bind(this)} /> <input type="submit" value="Update request" /> </form> ) } } export default TimeForm; 随着这些组件的创建,让我们在浏览器中加载我们的应用程序后, 获取数据正如我们昨天所说的,我们将使用 我们将建立我们将请求的 url,因为它代表了我们将在服务器上请求的时间查询。 我已经在 慢性 api 服务器接受几个变量,我们将在表单中进行自定义。它将采取的时区与一个慢性的消息。我们将简单地开始,并问慢性库的 class App extends React.Component { constructor(props) { super(props); this.state = { currentTime: null,msg: 'now',tz: 'PST' } } // ... getApiUrl() { const {tz,msg} = this.state; const host = 'https://andthetimeis.com'; return host + '/' + tz + '/' + msg + '.json'; } // ... export default App; 现在,当我们调用 让我们更新我们的 class App extends React.Component { // ... fetchCurrentTime() { fetch(this.getApiUrl()) .then(resp => resp.json()) .then(resp => { const currentTime = resp.dateString; this.setState({currentTime}) }) } // ... } 今天我们项目的最后一部分是从窗体中获取数据,以更新父组件。即,当用户更新 当一个状态在窗体组件上发生变化时,它将调用一个称为 事实上,我们将只调用 class App extends React.Component { // ... handleChange(newState) { this.setState(newState); } // ... } 最后,当用户提交表单时 (按下按钮 或者 按 回车 键在输入字段中单击),我们将希望对时间提出另一个请求。这意味着我们可以定义我们的 class App extends React.Component { // ... handleFormSubmit(evt) { this.fetchCurrentTime(); } // ... } 尝试运行演示和传递不同的慢速选择。真的很有趣 在任何情况下,今天我们做了相当多的工作,获取远程数据到我们的应用程序。然而,我们只有一页的单页应用程序。如果我们想在我们的应用程序中显示不同的页面呢?明天,我们将开始在我们的应用程序中添加多个页面,以便我们可以查看不同的视图。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |