[英]React, Solidity, Ethereum: I can't create a React button that correctly calls a smart contract function
So, I am currently developing a back-end and a front-end to a dApp. 因此,我目前正在开发dApp的后端和前端。
The back-end is my Solidity-written smart contract, which works perfectly fine when I use and test it on Truffle. 后端是我用Solidity编写的智能合约,当我在Truffle上使用和测试它时,它可以很好地工作。 I've created a struct, and in the contract I use a list of this struct.
我创建了一个结构,在合同中,我使用了该结构的列表。
struct Offre {
address manager;
string content;
uint number;
}
Offre[] public offres;
One of my function returns the content of any given element of this list. 我的函数之一返回此列表中任何给定元素的内容。
function getOfferNumber(uint i) public view returns (string r) {
return offres[i].content;
}
So, I've tested this function in Truffle and, as expected, it returns the string I want it to return. 因此,我已经在Truffle中测试了此函数,并且按预期方式,它返回了我想要返回的字符串。
Now, I am trying to create a button in React where I can : indicate a number i, click the button to get the content of the offer number i. 现在,我试图在React中创建一个按钮,我可以在其中:指示数字i,单击该按钮以获取要约编号i的内容。
In my App.js, I have my state : 在我的App.js中,我的状态是:
state = {
...
number: '',
offerToCheck: '',
};
The function I'm calling with my button goes like this 我用按钮调用的功能是这样的
checkMessage() {
const offerToCheck = DemandeOffre.methods.getOfferNumber(this.state.number).call();
this.setState({ offerToCheck });
}
where this.state.number is the number I am giving him in the render(): 其中this.state.number是我在render()中给他的数字:
<div>
<label>Check Offer</label>
<input
value={this.state.number}
onChange={event => this.setState({ number: event.target.value })}
/>
</div>
<button onClick={this.checkMessage}>Check</button>
<p> The offer you checked goes as follows : {this.state.offerToCheck}. </p>
When I try to use it like that, I get an "TypeError: this is undefined"
error by React on the const offerToCheck = DemandeOffre.methods.getOfferNumber(this.state.number).call();
当我尝试像这样使用它时,我在
const offerToCheck = DemandeOffre.methods.getOfferNumber(this.state.number).call();
上const offerToCheck = DemandeOffre.methods.getOfferNumber(this.state.number).call();
React的"TypeError: this is undefined"
错误const offerToCheck = DemandeOffre.methods.getOfferNumber(this.state.number).call();
line. 线。
I've tried various thing, like for example using a bind(this). 我已经尝试了各种方法,例如使用bind(this)。 When I do this, clicking on the button simply gets me to a blank page.
当我这样做时,单击按钮只会使我进入空白页。
I am not really familiar with react, so I don't know if I've explained my problem thoroughly. 我对反应不是很熟悉,所以我不知道我是否已经彻底解释了我的问题。 Hope I will find a solution.
希望我能找到解决方案。
In order for this
to not be undefined you can define the function like so: checkMessage = () => {...}
. 为了避免
this
,您可以像下面这样定义函数: checkMessage = () => {...}
。 Also, the call to the contract returns a promise so to get the value from there you have to use await
or .then()
, you can read more here . 另外,对合同的调用会返回一个承诺,因此要从那里获取价值,您必须使用
await
或.then()
,您可以在此处阅读更多内容。 The way you do it now the offerToCheck
variable will not have the correct value when you use it to set the state of the component. 现在,您使用的方式在使用
offerToCheck
变量设置组件状态时将没有正确的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.