- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用 React 制作一个天气小部件。
我从 OpenWeatherMap 获取的数据: http://openweathermap.org/api
我的问题是(或者是)渲染函数被立即调用。但 OpenWeatherMap API 请求的数据尚不可用。因此解释器会抛出错误,因为访问的 props 尚未定义。
我找到了一种解决方法,将渲染函数的 return 语句放入 if-else 中:
var React = require('react');
var DayInfos = require('./DayInfos.jsx');
var MainInfoPanel = require('./MainInfoPanel.jsx');
var WeatherApp = React.createClass({
getInitialState: function() {
return {
data: null
}
},
componentWillMount: function() { // Call before the initial rendering
var apiData = new Promise(function(resolve, reject) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
resolve(request.responseText);
} else {
reject('HTTP request on openweathermap has failed.');
}
}
}
request.open('get', 'http://api.openweathermap.org/data/2.5/forecast?q=London,us&mode=json&appid=efcd313fa7a139f2fb20de306648eb8d');
request.send();
});
apiData.then(function(weatherData) {
this.setState({
data: JSON.parse(weatherData)
});
}.bind(this), function(message) {
console.log(message);
});
},
render: function() {
if (this.state.data) { // Make sure the data is available.
return (
<div className="weather-app" >
<MainInfoPanel city={ this.state.data.city } today={ this.state.data.list[0] } />
<DayInfos />
<DayInfos />
<DayInfos />
<DayInfos />
</div>
);
} else {
return null;
}
}
});
module.exports = WeatherApp;
我现在工作,但我不确定我是否以正确的方式处理问题。
实际上我认为使用的函数 componentWillMount ( https://facebook.github.io/react/docs/component-specs.html#mounting-componentwillmount ) 可以解决这个问题。但显然它只是触发请求。其余的继续运行,无需等待数据到达。
因此,我向更有经验的 React 开发人员提出问题:
如何延迟初始渲染直到请求的数据可用?
或者:
避免因缺乏数据而导致应用程序崩溃的正确策略是什么?
最佳答案
您需要将标志条件最初设置为 false,并在数据到达时将其设置为 true。
var React = require('react');
var DayInfos = require('./DayInfos.jsx');
var MainInfoPanel = require('./MainInfoPanel.jsx');
var WeatherApp = React.createClass({
getInitialState: function() {
return {
data: null,
flag:false, //add flag
}
},
componentWillMount: function() { // Call before the initial rendering
var apiData = new Promise(function(resolve, reject) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
resolve(request.responseText);
} else {
reject('HTTP request on openweathermap has failed.');
}
}
}
request.open('get', 'http://api.openweathermap.org/data/2.5/forecast?q=London,us&mode=json&appid=efcd313fa7a139f2fb20de306648eb8d');
request.send();
});
apiData.then(function(weatherData) {
this.setState({
data: JSON.parse(weatherData),
flag:true // update state
});
}.bind(this), function(message) {
console.log(message);
});
},
render: function() {
return flag ?
(
<div className="weather-app" >
<MainInfoPanel city={ this.state.data.city } today={ this.state.data.list[0] } />
<DayInfos />
<DayInfos />
<DayInfos />
<DayInfos />
</div>
)
:
null;
}
}
});
module.exports = WeatherApp
现在,如果您不想使用单独的变量,您也可以使用 data.length 属性进行跟踪。
但是这里你需要将数据分配为数组或对象类型,data=[] 或 data={}。然后你的渲染将是这样的
render: function() {
return data.length > 0 ?
(
<div className="weather-app" >
<MainInfoPanel city={ this.state.data.city } today={ this.state.data.list[0] } />
<DayInfos />
<DayInfos />
<DayInfos />
<DayInfos />
</div>
)
:
null;
}
}
});
关于javascript - react : How to make sure that requested data are available before rendering?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673745/
我正在使用下面的代码但收到警告, bool versionSupports = (@available(iOS 10, *)); @available does not guard availabil
我需要检查 Xamarin.iOS 中的 API 可用性 - 在 Objective-C 或 Swift 中我可以使用这些调用: if (@available(iOS 13, *)) 或 if #av
我遇到了一个我似乎不明白的奇怪问题。我正在制作一个从文件中读取数据的程序。但是当我读取数据时,我立即收到 EOFException。 所以我用 .available() 检查文件是否为空。我得到了一个
Swift 2.0 允许使用 @available 或 #available 进行可用性检查,但是使用 @available 和 有什么区别#可用? 最佳答案 您可以使用 if #available
我刚刚开始学习 angularjs 我尝试了这段代码:在文件 angularmy.js var myname = angular.module("myModule",[]); myname.contr
我有一个 USB 麦克风和扬声器适配器连接到 raspberry pi 3。我已经在 alsamixer 上设置了所有内容。我也设置了pcm.!default sysdefault:0在文件中 .as
import requests import time import csv import ast import sys import mysql.connector config = { 'user
我想让页脚的宽度与浏览器无关。 对于 Mozilla,我想使用 -moz-available 的值,当用户使用 Opera 时,那么 CSS 应该从 -webkit-fill-available 中获
如果您的代码需要仅在 macOS 10.12 或更高版本中可用的功能,但您希望您的代码也部署到更早的系统版本,您可以使用 @available在 Objective-C 中: if (@availab
我正在使用 Element Query允许在任何元素上使用 @media queries 的元素。这是它的处理方式: #foo:media(min-available-width:350px and
我正在尝试安装 oracle 19c,但在安装过程中遇到了与内存相关的问题“[INS-35179] 当前可用内存小于创建数据库所需的可用内存 (6,537MB)”。我仔细检查了所有先决条件,例如超过
我正在编写代码以使用此页面中的文档跟踪现场请求:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-spot-instances-bid-
我仔细阅读了 Kubernetes 文档 here关于扩展 imagefs.available 的默认 15%和其他参数,但没有说明如何设置,我已经安装了具有以下配置的 RKE(Rancher Kub
我想进入PAPI。我在Debian GNU/Linux上有5.3.2.0版。 papi_avail告诉我没有可用的硬件事件: $ papi_avail Available events and har
我目前正在构建一个混合云解决方案,需要将消息写入队列以供稍后处理。队列具有高可用性(99.999+% 的正常运行时间)是绝对必要的。 我的选择是将消息读/写到本地 ZeroMQ 高可用性对或 Azur
在 Mac OS X Leopard 中使用 Eclipse Helios 并调试调用 fsf gdb 7.1 的 C++ 代码,调试停止在 main 的第一行。然后在第一步之后我得到 No sour
无论如何,是否可以将 UIWebView 与针对 tvos 的应用程序一起使用?这个苹果文档,UIWebView Class Reference ,会建议否则不是吗?或者我只是解释错了? UIWebV
我想删除以下数据框中的“不可用”,但是当我使用以下代码将 Number 更改为数字时,“不可用”变为 4: c1 data 是一个 factor 列。 当您将一个因子直接转换为numeric 时,生成
PushKit 在 iOS 11 中提供了一种新方法,旨在取代 iOS 10 中的方法。 使用 iOS 11 作为基础 SDK 构建时无法使用 iOS 10 方法(我当前使用的是 Xcode 9.2B
不确定使用@rename 指令的所有方法。 我正在尝试重命名一个方法 @available(*, deprecated, renamed: "setValueInTable") public func
我是一名优秀的程序员,十分优秀!