[英]Render after API call ReactJS
Alexnm Shows the terribly disgusting architecture you must provide to simply have a server wait before an API call is complete before a server responds to a client. Alexnm展示了您必须提供的非常恶心的架构,以便在服务器响应客户端之前让服务器在 API 调用完成之前等待。
He shows that you must first document each component of your entire app in a single, non-hierarchical flat list, then filter through every single component to see if a component has a API "data-requirement" for every single request , then store the output of that API request in a global redux object (also disgusting), then somehow pass that back to the object in the hopes it doesn't collide with other requests.他表明,您必须首先将整个应用程序的每个组件记录在一个单一的、非分层的平面列表中,然后过滤每个组件以查看某个组件是否具有针对每个请求的 API“数据需求”,然后存储该 API 请求在全局redux对象中的输出(也很恶心),然后以某种方式将其传递回对象,希望它不会与其他请求发生冲突。
This is because ReactJS " lifecycle " hooks aren't actually lifecycle hooks at all, and is instead "fast and free" (and fun) .这是因为 ReactJS 的“ 生命周期”钩子实际上根本不是生命周期钩子,而是“快速且免费”(且有趣) 。
I don't really care for any of these libraries, ES[xyz] syntaxes, and strange philosophies, but what I would like to do is return the an http response after and API call is made, ie我并不真正关心这些库、ES[xyz] 语法和奇怪的哲学中的任何一个,但我想做的是在调用 API之后返回一个 http 响应,即
render() {
// do an API call.
// return a response.
}
I've seen uses if fetch(xyz).then(() => {})
and so many other colorful brackets and functions where I'm not totally sure where parameters go in or where they go out, and I can't seem to figure out how to simply hold off the next line of code before the previous line of code is complete.我见过使用 if
fetch(xyz).then(() => {})
和许多其他彩色括号和函数,我不完全确定参数从哪里进入或从哪里出去,我不能似乎弄清楚如何在上一行代码完成之前简单地推迟下一行代码。 In then(() => {})
, which is syntaticly beautiful, I'm unable to return a value in any way at all.在
then(() => {})
,这在语法上很漂亮,我根本无法以任何方式返回值。 It's called a function , but I'm having difficulty achieving anything other than a side side-effect , and am unable to return a result of any kind.这就是所谓的功能,但我有困难,实现比侧副作用其他任何东西,我无法返回任何结果。
Does anyone know how to make the render()
, on it's own, wait for an API call?有谁知道如何让
render()
自己等待 API 调用?
As for philosophies - you may want to read more on flux patterns - which were architectural design principles conjoured at Facebook.至于哲学——你可能想阅读更多关于通量模式的内容——这是 Facebook 提出的架构设计原则。 The efforts were to handle global state in a Single Page Application, or SPA.
努力是在单页应用程序或 SPA 中处理全局状态。
Per your current syntax issue - you may utilize promises with arrow functions.根据您当前的语法问题 - 您可以使用带有箭头函数的承诺。 Promises were introduced in ES6 as a solution for handling asynchronous requests cleaner.
ES6 中引入了 Promise 作为处理异步请求的解决方案。
Using your example fetch(xyz).then(() => {})
one may execute requests synchronously by:使用您的示例
fetch(xyz).then(() => {})
可以通过以下方式同步执行请求:
render() {
Promise.resolve(fetch(xyz))
.then((response) => {
// Do something with this response - for example..
if (response.data) {
axios.get("http://google.com")
.then((response) => {
console.log(response.data);
})
.catch((err) => {
// Do something with this error
console.log(err);
})
}
})
})
return (
<div>
</div>
)
}
I won't get into time complexities - but the above solution will fire off the fetch(xyz)
request, then once that requet has completed - we fire off the next request.我不会涉及时间复杂性 - 但上述解决方案将触发
fetch(xyz)
请求,然后一旦该请求完成 - 我们就会触发下一个请求。 This is called promise chaining - more information may be found here: https://javascript.info/promise-chaining这称为承诺链 - 可以在此处找到更多信息: https : //javascript.info/promise-chaining
If your goal is to simply make a call and use the data once the component has rendered, or mounted, you may do something cleaner and react friendly (non blocking) like the example below:如果您的目标是在组件渲染或安装后简单地进行调用并使用数据,您可以做一些更简洁的操作并做出友好的反应(非阻塞),如下例所示:
import React, {Component} from 'react';
class Example extends Component {
constructor(props) {
super(props);
this.state = {
data: ''
};
}
componentDidMount() {
Promise.resolve(fetch(xyc))
.then((response.data) => {
this.setState({
data:response.data
});
})
};
render() {
const data = this.state
return (
{data}
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.