2017-07-28 10 views
0

に設定した後は未定義:リアクト状態このプレゼンテーションファイルですcomponentDidMount

import React, { Component } from 'react' 

class CreateZone extends Component { 
    constructor(){ 
     super() 
      this.state = { 
       zone: { 
        name: '', 
        zipCodes: [] 
       } 
      } 
    } 

    newZone(event){ 
     let newZone = Object.assign({}, this.state.zone) 
     newZone[event.target.id] = event.target.value 
     this.setState({ 
      zone: newZone 
     }) 
    } 
    submitZone(event){ 
     this.props.onCreate(this.state.zone) 
    } 

    render(){ 
     return(
      <div> 
      <input id="name" onChange={this.newZone.bind(this)} className="form-control" type="text" placeholder="Enter Zone Name" /><br /> 
      <input id="zipCodes" onChange={this.newZone.bind(this)} className="form-control" type="text" placeholder="Enter Zip Code" /><br /> 
      <button onClick={this.submitZone.bind(this)} className="btn btn-danger">Submit Comment</button> 
      </div> 
     ) 
    } 
} 
export default CreateZone 

これでコンテナファイル:

import React, { Component } from 'react' 
import { Zone, CreateZone } from '../presentation' 
import { APImanager } from '../../utils' 

class Zones extends Component { 
    constructor(){ 
     super() 
     this.state = { 
      zone: { 
       name: '', 
       zipCodes: '', 
       numComments: '' 
      }, 
      list: [] 
     } 
    } 

    componentDidMount(){ 
     console.log('componentDidMount') 
     APImanager.get('/api/zone', null, (err, response) => { 
      if(err){ 
       alert('Error in zones: '+err.message) 
       return 
      } 

      this.setState({ 
       list: response.results 
      }) 
     }) 
    } 

    submitZone(zone){ 
     let newZone = Object.assign({}, zone) 

     APImanager.post('/api/zone', newZone, (err, response) => { 
      if(err) { 
       alert('ERROR in New Zone: '+err.message) 
       return 
      } 
      console.log('NewZone: '+JSON.stringify(response)) 
      let updatedList = Object.assign([], this.state.list) 
      updatedList.push(response.result) 
      this.setState({ 
       list: updatedList 
      }) 


     }) 
    } 

    render(){ 

     const listItems = this.state.list.map((zone, i) => { 
      return (
       <li key={i}> 
        <Zone currentZone={zone} /> 
       </li> 
      ) 
     }) 

     return(
      <div> 
       <ol> 
        {listItems} 
       </ol> 
       <div> 
        <CreateZone onCreate={this.submitZone} /> 
       </div> 
      </div> 
     ) 
    } 
} 
export default Zones 

が再レンダリングおよびコンソールログエラーがプロパティを読み取ることができません」ではありませんリアクト定義されていない 'リスト' ' フォームをプレゼンテーションファイルに移動する前にうまくいきました。これは私のための訓練であり、何が起こっているのか理解したいです

答えて

1

ゾーンのコンポーネントのsubmitZoneは "this"バインドされませんでした。だから、 "this.state"は未定義としていました。それは助けたありがとうだから、

<CreateZone onCreate={this.submitZone.bind(this)} /> 
+0

でライン

<CreateZone onCreate={this.submitZone} /> 

を交換

<button onClick={this.submitZone.bind(this)} className="btn btn-danger">Submit Comment</button> 

として "この" 同じバインド – eddbreuer

関連する問題