简体   繁体   English

React,Solidity,Ethereum:我无法创建可正确调用智能合约功能的React按钮

[英]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.

相关问题 智能合约:如何在 React 中获得 Solidity function 的回报? - Smart contract: How to get return of Solidity function in React? 通过javascript(web3.js)从部署的智能合约中调用特定的solidity函数 - Call specific solidity function from deployed smart contract within react through javascript (web3.js) Solidity:从我的智能合约中显示价值以做出反应的问题 - Solidity : Problem to display value from my smart contract to react 如何为javascript / truffle中的每个测试创建新的以太坊/实体合约 - how to create new ethereum/solidity contract for each test in javascript/truffle 尝试从 react js 中获取 Solidity 智能合约中的数据时返回 NULL 值 - NULL value returned when trying to fetch data from solidity smart contract from react js 在 JS 循环内调用以太坊智能合约函数不同步 - Calling Ethereum Smart contract function inside a JS loop is not synchronize 在固态合约实例承诺中反应setState - React setState inside solidity contract instance promises 如何从 Javascript 向 Solidity 智能合约 function 输入数据? - How to input data to a solidity smart contract function from Javascript? 我们可以更新部署在 Hedera 测试网中的 Solidity 智能合约吗? - Can we update solidity smart contract which is deployed in Hedera TestNet? Solidity:在智能合约中填充结构 - Solidity: populating a struct in a smart contract
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM