得到节点端点数据并通入反应前端
我从访问本地节点服务器的GET请求,并正在接受databack到反应代码,但我不知道如何再使用接收到的数据。完整的小白与反应和节点。任何帮助,不胜感激
const https = require("https");
const url =
"http://localhost:9001/products";
https.get(url, res => {
res.setEncoding("utf8");
let body = ""
res.on("data", data => {
body += data;
});
res.on("end", () => {
console.log(
body
);
});
});
控制台显示所有要退回的数据,但现在我想使用这些数据,将其传递到使用出口成本
export default data;
每当我试图给身体的续URL代码之外,它说不确定。我如何抓住要导出的数据
谢谢
回答如下:完美的使用情况下的Container Component:
你应该把API调用父/容器组件,然后将响应数据存储在状态。然后,您可以将数据传递下来的道具任何子女:
const https = require("https");
export default class FetchData extends Component {
constructor () {
super();
this.state = {
data: null
};
}
componentDidMount() {
this.fetchData();
}
fetchData = () => {
const url = "http://localhost:9001/products";
https.get(url, res => {
res.setEncoding("utf8");
let body = ""
res.on("data", data => {
body += data;
});
res.on("end", () => {
// Store data to state
this.setState({
data: body
});
});
});
};
render() {
return (
<div>
<Child1 data={this.state.data} />
<Child2 data={this.state.data} />
<Child3 data={this.state.data} />
</div>
)
}
}
得到节点端点数据并通入反应前端
我从访问本地节点服务器的GET请求,并正在接受databack到反应代码,但我不知道如何再使用接收到的数据。完整的小白与反应和节点。任何帮助,不胜感激
const https = require("https");
const url =
"http://localhost:9001/products";
https.get(url, res => {
res.setEncoding("utf8");
let body = ""
res.on("data", data => {
body += data;
});
res.on("end", () => {
console.log(
body
);
});
});
控制台显示所有要退回的数据,但现在我想使用这些数据,将其传递到使用出口成本
export default data;
每当我试图给身体的续URL代码之外,它说不确定。我如何抓住要导出的数据
谢谢
回答如下:完美的使用情况下的Container Component:
你应该把API调用父/容器组件,然后将响应数据存储在状态。然后,您可以将数据传递下来的道具任何子女:
const https = require("https");
export default class FetchData extends Component {
constructor () {
super();
this.state = {
data: null
};
}
componentDidMount() {
this.fetchData();
}
fetchData = () => {
const url = "http://localhost:9001/products";
https.get(url, res => {
res.setEncoding("utf8");
let body = ""
res.on("data", data => {
body += data;
});
res.on("end", () => {
// Store data to state
this.setState({
data: body
});
});
});
};
render() {
return (
<div>
<Child1 data={this.state.data} />
<Child2 data={this.state.data} />
<Child3 data={this.state.data} />
</div>
)
}
}