Я новичок в реагировании и программировании прочности.Я написал простой код солидности, который добавляет файлы (каждый файл известен по их описанию и хешу) в список файлов.Реактивная часть добавления файлов работает нормально, я могу добавлять файлы через их описание и хэш.Проблема, с которой я сталкиваюсь, заключается в том, что я хочу каждый раз, когда я добавляю новый файл, я получаю его описание и отображаемый хэш.
Это часть реакции добавления файла
import React, { Component } from 'react';
import Layout from '../../components/Layout';
import { Form, Button, Input, Message } from 'semantic-ui-react'
import auction from '../../src/auction';
import web3 from '../../src/web3';
import { Router} from '../../routes';
class FileNew extends Component {
state = {
description: '',
hash:'',
fileCounts:'',
value: ''
};
async componentDidMount() {
const manager = await auction.methods.manager().call();
const fileCounts = await auction.methods.FileCounts().call();
const accounts = await web3.eth.getAccounts(console.log);
this.setState({ manager, fileCounts, accounts });
}
onSubmit = async (event) => {
this.setState ({ loading: true});
const accounts = await web3.eth.getAccounts().catch((e) => { console.log(e); });
await auction.methods.addFile(this.state.description,this.state.hash).send({from : accounts[0]});
Router.pushRoute('/files/list');
this.setState ({ loading: false});
};
render() {
return (
<Layout>
<h1> Create new file </h1>
<Form onSubmit={this.onSubmit}>
<Form.Field>
<label>description</label>
<Input label="First Name" labelPosition="right" value={this.state.description}
onChange={event =>
this.setState ({description: event.target.value})} />
</Form.Field>
<Form.Field>
<label>hash</label>
<Input label="Last Name" labelPosition="right" value={this.state.hash}
onChange={event =>
this.setState ({hash: event.target.value})}/>
</Form.Field>
<Button loading={this.state.loading} type='submit'>Submit</Button>
<p> The number of files is {this.state.fileCounts} </p>
<p> Version 2 The manager of this app is {this.state.manager} </p>
<p> Your current session is {this.state.accounts} </p>
</Form>
</Layout>
);
}
}
export default FileNew
solidity
struct file {
string description;
string hash;
}
uint public FileCounts = 0;
uint public FileId = 0;
mapping (uint => file) files;
event _fileCreated(uint indexed id);
event _fileUpdated(uint indexed id);
function addFile(string _description, string _hash) public {
FileCounts++;
files[FileId] = file(_description, _hash);
FileId++;
emit _fileCreated(FileId);
}
function deleteFile(uint id) public restricted {
delete files[id];
FileCounts--;
}
function getFile(uint id) external constant returns (string, string) {
file storage F = files[id];
return (F.description, F.hash);
}
function FileCount() public view returns (uint) {
return FileCounts;
}
function getFileDesc(uint id) external constant returns (string) {
return (files[id].description);
}
Заранее спасибо!