【发布时间】:2017-12-21 17:17:39
【问题描述】:
我目前正在尝试使用 React 以表单形式从用户那里获取完整的输入。我需要获取这些输入,然后将它们存储起来,以便我可以将这些值传递给另一个函数。目前,我一直在尝试使用不受控制的输入但没有成功,但也尝试过受控输入但没有任何成功。有任何想法吗?我必须将这些值传递给函数peopleContract.addPerson(this._firstName, this._lastName, this._email, {from: accounts[1], gas: 3000000})
代码如下(注释为受控输入方式):
import React from 'react';
import Web3 from 'web3';
//Declaring the ethereum client (initializing) with the url in which the testrpc is running
var ETHEREUM_CLIENT = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
//These could be dynamically added through input fields, but hard coding for now
var peopleContractABI = [{"constant":true,"inputs":[],"name":"getPeople","outputs":[{"name":"","type":"bytes32[]"},{"name":"","type":"bytes32[]"},{"name":"","type":"bytes32[]"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"people","outputs":[{"name":"firstName","type":"bytes32"},{"name":"lastName","type":"bytes32"},{"name":"email","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_firstName","type":"bytes32"},{"name":"_lastName","type":"bytes32"},{"name":"_email","type":"bytes32"}],"name":"addPerson","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"}]
var peopleContractAddress = '0xb1a711f4e1250761b85be7bb4478c07d256b8225'
var peopleContract = ETHEREUM_CLIENT.eth.contract(peopleContractABI).at(peopleContractAddress)
//Need to create a variable named accounts in order to know which account
//to make the transactions from
var accounts = ETHEREUM_CLIENT.eth.accounts
//Creating the dynamic input fields for the user to input his/her data
export class Form extends React.Component{
handleSubmitClick = () => {
const firstName = this._firstName.value;
const lastName = this._lastName.value;
const email = this._email.value;
//do something with these variables
}
/*
handleChange(event) {
this.setState({[key]: event.target.value});
}
*/
/*
handleChange(event) {
this.setState({[event.target.name]: event.target.value});
}
handleSubmit(event) {
alert('A user was submitted: ' + this.state.firstName + this.state.lastName + this.state.email);
event.preventdefault();
*/
/*
if((this.state.firstName==!"") && (this.state.lastName==!"")&& (this.state.email==!"")){
peopleContract.addPerson(this.state.firstName, this.state.lastName, this.state.email, {from: accounts[1], gas: 3000000})
// after you subimt values clear state
this.setState({
firstName: this.state.firstName,
lastName: this.state.lastName,
email: this.state.email
})
}else{
// render error
alert('Some fields are mandatory');
}
}
*/
/*
componentWillMount(){
peopleContract.addPerson(this._firstName, this._lastName, this._email, {from: accounts[1], gas: 3000000})
}
*/
render() {
peopleContract.addPerson(this._firstName, this._lastName, this._email, {from: accounts[1], gas: 3000000})
return(
<form>
<div>
<h4>Name</h4>
<input
type="text"
ref={input => this._firstName = input} />
</div>
<div>
<h4>Last Name</h4>
<input
type="text"
ref = {input2 => this._lastName = input2} />
</div>
<div>
<h4>Email</h4>
<input
type="text"
ref = {input3 => this._email = input3} />
</div>
<button onClick={this.handleSubmitClick}>Submit</button>
</form>
);
}
}
【问题讨论】:
-
你遇到了什么错误?
-
它告诉我我没有将所需的参数传递给函数,它应该是免费的,这意味着我需要以某种方式存储输入
标签: javascript reactjs rendering react-redux text-rendering