【发布时间】:2017-12-16 12:09:10
【问题描述】:
我有一个问题,我在 render() 中有一个函数,它将输出每个输入文本,也就是说,它逐个字母而不是逐字打印。我试图将函数放在 render() 之外,但它不起作用。代码如下:
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 = '0xa0b4dccf81cb4bc6cdb890637dc02b62a7a35b66'
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{
constructor(props){
super(props);
this.state = {
firstName: "",
lastName: "",
email: "",
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event, key) {
this.setState({[key]: event.target.value});
}
handleSubmit(event) {
alert('A user was submitted: ' + this.state.firstName + this.state.lastName + this.state.email);
event.preventdefault();
}
/*Creating so that person can be added
componentWillMount(){
this.setState({
firstName: this.state.firstName,
lastName: this.state.lastName,
email: this.state.email
})
}
*/
render() {
peopleContract.addPerson(this.state.firstName, this.state.lastName, this.state.email, {from: accounts[1], gas: 3000000})
return(
<form onSubmit={this.handleSubmit}>
<h4>Name</h4>
<input
type="text"
placeholder="e.g. Bob"
value={this.state.firstName}
onChange={event => this.handleChange(event, 'firstName')} />
<div>
<h4>Last Name</h4>
<input
type="text"
placeholder="e.g. Stark"
value={this.state.lastName}
onChange={event => this.handleChange(event, 'lastName')}/>
</div>
<div>
<h4>Email</h4>
<input
type="text"
placeholder="e.g. bobstark@gmail.com"
value={this.state.email}
onChange={event => this.handleChange(event, 'email')}/>
</div>
<input
type = "submit"
name = "Submit"
/>
</form>
);
}
}
【问题讨论】:
-
您只想在一个单词完成后才调用
peopleContract.addPerson?似乎有点奇怪。人听起来像一个实体,你为什么要在每一个单词变化时都调用 add entity? -
这是为了将用户的输入作为名字、姓氏和电子邮件添加到函数所需的参数中
标签: javascript reactjs rendering react-redux text-rendering