【发布时间】:2019-05-13 21:58:41
【问题描述】:
这是上一个帖子的后续问题
How to return json data to a react state?
我的反应组件将axios.post 发送到express 服务器。服务器使用web3 在Ethereum 上签署交易。 Ethereum 返回以下 json 对象。值得注意的是,返回 json 需要一些时间(几秒到几分钟,具体取决于矿工):
{ blockHash: '0xcd08039bac40e2865886e8f707ce9b901978c339d3abb81516787b0357f53fbd',
blockNumber: 4611028,
...other data...
transactionHash: '0x12c65523743ed169c764553ed2e0fb2af1710bb20a41b390276ffc2d5923c6a9',
transactionIndex: 1 }
这是我用来制作axios.post 并设置状态的代码:
import React from "react";
import PaypalExpressBtn from "react-paypal-express-checkout";
import axios from "axios";
export default class Pay extends React.Component {
constructor(props) {
super(props);
this.state = {
items: {}
};
}
render() {
const onSuccess = payment => {
axios
.post("http://compute.amazonaws.com:3000/", {
value: this.props.value,
fileName: this.props.fileName,
hash: this.props.hash
})
.then(response => console.log(response.data))
.catch(function(error) {
console.log(error);
});
console.log(payment);
};
let env = "sandbox"; // you can set here to 'production' for production
let currency = "USD"; // or you can set this value from your props or state
let total = 3.33; // same as above, this is the total amount (based on
const client = {
sandbox:
"...key...",
production: "YOUR-PRODUCTION-APP-ID"
};
return (
<div>
<PaypalExpressBtn
onSuccess={onSuccess}
/>
</div>
);
}
}
当我console.log({ items: this.state.items}) 时,我返回了看似无穷无尽的构造函数和道具。
我试过了
this.setState({ items : items.transactionHash }); 和 console.log(
{ items: this.state.items.transactionHash}),都不起作用。
我需要做的是set.state,只有上面json中的transactionHash,没有别的。
非常感谢!
【问题讨论】:
-
你能发布你当地的结构吗?
-
是的,我会将它包含在编辑中,并包含一些我尝试过的东西。谢谢。