2017-07-11 4 views
1

ユーザーが登録フォームに記入できるようにアプリケーションを作成しています。このユーザーの名前とその他のパラメータがReact JSを使用して表示されます。これはスマート契約からも得られます。私はテキスト入力に入力した後に未定義の入力があるという問題を抱えています。それは状態と関係がありますが、それを修正する方法がわからないと思われます。React JS:ユーザーからの入力を取得できず、入力時に未定義になる

import React, { Component } from 'react'; 
import logo from './logo.svg'; 
import dxclogo from './dxclogo.png'; 
import './App.css'; 
import Web3 from 'web3'; 
import _ from 'lodash' 
import { Form } from './Form'; 

//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":"age","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_firstName","type":"bytes32"},{"name":"_lastName","type":"bytes32"},{"name":"_age","type":"bytes32"}],"name":"addPerson","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"}] 

var peopleContractAddress = '0x7d58e54b812b1f7c5c47f0a85af5f9f6a1d3f5c8' 

var peopleContract = ETHEREUM_CLIENT.eth.contract(peopleContractABI).at(peopleContractAddress) 

class App extends Component{ 
    constructor(props){ 
    super(props); 
    this.state = { 
     firstNames: [], 
     lastNames: [], 
     ages: [] 
    } 
    } 

    componentWillMount(){ 
    var data = peopleContract.getPeople() 
    this.setState({ 
     firstNames: String(data[0]).split(','), 
     lastNames: String(data[1]).split(','), 
     ages: String(data[2]).split(',') 
    }) 
    } 

    render() { 
    var TableRows = [] 

    _.each(this.state.firstNames, (value, index) => { 
    TableRows.push(
     <tr> 
     <td>{ETHEREUM_CLIENT.toAscii(this.state.firstNames[index])}</td> 
     <td>{ETHEREUM_CLIENT.toAscii(this.state.lastNames[index])}</td> 
     <td>{ETHEREUM_CLIENT.toAscii(this.state.ages[index])}</td> 
     </tr> 
    ) 
    }) 

    return (
     <div className="App"> 
     <div className="App-header"> 
      <img src={logo} className="App-logo" alt="logo" /> 
      <img src = {dxclogo} className ="App-dxclogo" alt="dxclogo" /> 
      <h1>Hotel Room Booking dApp</h1> 
     </div> 
     <div> 
      <Form /> 
      <table className="App-tablePeople"> 
      <thead> 
       <tr> 
       <th>First Name </th> 
       <th>Last Name </th> 
       <th>Age </th> 
       </tr> 
      </thead> 
      <tbody> 
       {TableRows} 
      </tbody> 
      </table> 
     </div> 
     </div> 
    ); 
    } 
} 

export default App; 

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":"age","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_firstName","type":"bytes32"},{"name":"_lastName","type":"bytes32"},{"name":"_age","type":"bytes32"}],"name":"addPerson","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"}] 

var peopleContractAddress = '0x7d58e54b812b1f7c5c47f0a85af5f9f6a1d3f5c8' 

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: "", 
    age: "", 
    } 

    this.handleChange = this.handleChange.bind(this); 
    this.handleSubmit = this.handleSubmit.bind(this); 
    } 

    handleChange(event) { 
    this.setState({firstName: event.target.firstName}); 
    this.setState({lastName: event.target.lastName}); 
    this.setState({age: event.target.age}); 

    } 

    handleSubmit(event) { 
    alert('A user was submitted: ' + this.state.firstName + this.state.lastName + this.state.age); 
    event.preventdefault(); 
    } 


//Creating so that person can be added 
    componentWillMount(){ 
    peopleContract.addPerson(this.state.firstName, this.state.lastName, this.state.age, {from: accounts[1], gas: 3000000}) 
    } 

    render() { 
    return(
     <form onSubmit={this.handleSubmit}> 
     <h4>Name</h4> 
     <input 
      type="text" 
      placeholder="Name, e.g. Javier" 
      firstName={this.state.firstName} 
      onChange={this.handleChange} /> 
     <div> 
     <h4>Last Name</h4> 
      <input 
      type="text" 
      placeholder="Last Name, e.g. Hernandez" 
      lastName={this.state.lastName} 
      onChange={this.handleChange}/> 
     </div> 
     <div> 
     <h4>Age</h4> 
      <input 
      type="text" 
      placeholder="Age, e.g. 35" 
      age={this.state.age} 
      onChange={this.handleChange}/> 
     </div> 
     <input 
      type = "submit" 
      name = "Submit" 
      /> 
     </form> 
    ); 
    } 
} 
+0

'' event.target.firstName''はどうなっていますか?私はあなたが 'event.target.value'を意味すると思います – azium

+0

' firstName'はコード内の 'value'と同じものであることを意味します。それは私が思ったものです – jh5614

+0

' event.target'はDOMノードを参照しています。 'firstName'プロパティはありません。 'event.target.value'を使用して、値が入ってくるのを確認してください。 – azium

答えて

0

Form.jsと呼ばれるコード、あなたは正しい軌道に乗っているが、あなたのhandleChange:ここ

はApp.jsと呼ばれる親コード、のコードです関数は少しハードコーディングされています。それは、任意の入力に対して一般だように、より多くのパラメータを取る:あなたの入力の残りの部分など

<input 
    type="text" 
    placeholder="Name, e.g. Javier" 
    firstName={this.state.firstName} 
    onChange={event => this.handleChange(event, 'firstName')} 
/> 

と:

handleChange(event, key) { 
    this.setState({ [key]: event.target.value }) 
} 

更新しますonChangeハンドラは、一緒にその情報を渡します。それからあなたは提出することができ、あなたの書式には必要なすべての州が含まれます。そうでない場合は、そこでいくつかの検証を行うことができます。

+0

これは動作しますが、私は 'render(){ peopleContract.addPerson(this.state.lastName、this.state.lastName、this.state.age、{from:accounts [1])のように' peopleContract.addPerson'関数をレンダリングしています。 ]、ガス:3000000}) リターン( ' – jh5614

+0

ここであなたは個別の文字を取得していますか? – azium

+0

最新のコードで質問を更新できますか – azium

0

ここに最新のコードがあります。

App.js

import React, { Component } from 'react'; 
import logo from './logo.svg'; 
import dxclogo from './dxclogo.png'; 
import './App.css'; 
import Web3 from 'web3'; 
import _ from 'lodash' 
import { Form } from './Form'; 

//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":"age","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_firstName","type":"bytes32"},{"name":"_lastName","type":"bytes32"},{"name":"_age","type":"bytes32"}],"name":"addPerson","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"}] 

var peopleContractAddress = '0x6c6a4a3e14e25a31fcc323aed7e8395a7df56a6f' 

var peopleContract = ETHEREUM_CLIENT.eth.contract(peopleContractABI).at(peopleContractAddress) 

class App extends Component{ 
    constructor(props){ 
    super(props); 
    this.state = { 
     firstNames: [], 
     lastNames: [], 
     ages: [] 
    } 
    } 

    componentWillMount(){ 
    var data = peopleContract.getPeople() 
    this.setState({ 
     firstNames: String(data[0]).split(','), 
     lastNames: String(data[1]).split(','), 
     ages: String(data[2]).split(',') 
    }) 
    } 

    render() { 
    var TableRows = [] 

    _.each(this.state.firstNames, (value, index) => { 
    TableRows.push(
     <tr> 
     <td>{ETHEREUM_CLIENT.toAscii(this.state.firstNames[index])}</td> 
     <td>{ETHEREUM_CLIENT.toAscii(this.state.lastNames[index])}</td> 
     <td>{ETHEREUM_CLIENT.toAscii(this.state.ages[index])}</td> 
     </tr> 
    ) 
    }) 

    return (
     <div className="App"> 
     <div className="App-header"> 
      <img src={logo} className="App-logo" alt="logo" /> 
      <img src = {dxclogo} className ="App-dxclogo" alt="dxclogo" /> 
      <h1>Hotel Room Booking dApp</h1> 
     </div> 
     <div> 
      <Form /> 
      <table className="App-tablePeople"> 
      <thead> 
       <tr> 
       <th>First Name </th> 
       <th>Last Name </th> 
       <th>Age </th> 
       </tr> 
      </thead> 
      <tbody> 
       {TableRows} 
      </tbody> 
      </table> 
     </div> 
     </div> 
    ); 
    } 
} 

export default App; 

Form.js:

componentWillMount(){ 
    var data = peopleContract.getPeople() 
    this.setState({ 
     firstNames: String(data[0]).split(','), 
     lastNames: String(data[1]).split(','), 
     ages: String(data[2]).split(',') 
    }) 
    } 

    render() { 
    var TableRows = [] 

    _.each(this.state.firstNames, (value, index) => { 
    TableRows.push(
     <tr> 
     <td>{ETHEREUM_CLIENT.toAscii(this.state.firstNames[index])}</td> 
     <td>{ETHEREUM_CLIENT.toAscii(this.state.lastNames[index])}</td> 
     <td>{ETHEREUM_CLIENT.toAscii(this.state.ages[index])}</td> 
     </tr> 
    ) 
    }) 

いずれかの方法を使用して、ローカルホストのWeb内のテキストを出力するときに、私は個々の文字を取得しています、最新のコードはこれです

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":"age","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_firstName","type":"bytes32"},{"name":"_lastName","type":"bytes32"},{"name":"_age","type":"bytes32"}],"name":"addPerson","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"}] 

var peopleContractAddress = '0x6c6a4a3e14e25a31fcc323aed7e8395a7df56a6f' 

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: 0x0000000000000000000000000000000000000000000000000000000000000000, 
    lastName: 0x0000000000000000000000000000000000000000000000000000000000000000, 
    age: 0x0000000000000000000000000000000000000000000000000000000000000000, 
    } 

    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.age); 
    event.preventdefault(); 
    } 


/*Creating so that person can be added 
    componentWillMount(){ 
    peopleContract.addPerson(this.state.firstName, this.state.lastName, this.state.age, {from: accounts[1], gas: 3000000}) 
    } 
    */ 

    render() { 
    peopleContract.addPerson(this.state.firstName, this.state.lastName, this.state.age, {from: accounts[1], gas: 3000000}) 
    return(
     <form onSubmit={this.handleSubmit}> 
     <h4>Name</h4> 
     <input 
      type="text" 
      placeholder="Name, e.g. Javier" 
      value={this.state.firstName} 
      onChange={event => this.handleChange(event, 'firstName')} /> 
     <div> 
     <h4>Last Name</h4> 
      <input 
      type="text" 
      placeholder="Last Name, e.g. Hernandez" 
      value={this.state.lastName} 
      onChange={event => this.handleChange(event, 'lastName')}/> 
     </div> 
     <div> 
     <h4>Age</h4> 
      <input 
      type="text" 
      placeholder="Age, e.g. 35" 
      value={this.state.age} 
      name="age" 
      onChange={event => this.handleChange(event, 'age')}/> 
     </div> 
     <input 
      type = "submit" 
      name = "Submit" 
      /> 
     </form> 
    ); 
    } 
} 
関連する問題