2016-05-16 16 views
1

ReactJs cannot access propsの複製である可能性がありますが、これはまだ未定義になりません。Reactjsアクセス小道具を使用している子会社のJSONのオブジェクトプロパティ

しかし、私は構文どのようにフェッチオブジェクトプロパティなどで立ち往生しています:私の子コンポーネントで反応に小道具を使用して、以下のJSONレスポンスにオブジェクト「releasedata」S「」プロパティ。

注:私はスタックでたくさんの答えを見つけた配列ではありません。

JSON:

{ 
"id": "-559674239450219239", 
"key": "TASK-1", 
"name": "Hide old Releases", 
"projectId": "-5010744238007635986", 
"releasedata": {     <--- //Access releasedata Object using props not an array 
       "releaseId": "508054512004262880", 
       "name": "prog rel1",  <--- // Get name 
       "projectId": null, 
       "programId": "1" 
       }, 
"done": false, 
} 

マイコード:

親:

componentDidMount: function(){ 
     $.ajax({ 
      url: "http://localhost:8080/app/restapi/getreleasedata/"+Key+"?access_token="+accesstokenvalue+"" 
     }).then(function(data) { 
      this.setState({data: data}); 
         console.log(data); 
      }.bind(this)) 
    }, 
render: function(){ 
    return (
      <PbiHeader releaseData={this.state.data.releasedata} />     
    ) 
    } 
}); 

子コンポーネント:

var PbiHeader = React.createClass({ 
    render: function(){ 
    return (
        <tbody> 
        <tr> 
         <td>{this.props.releaseData.name}</td> //Here i am getting undefined and i am stuck with syntax to fetch object releaseData's name 
        </tr> 
        </tbody> 
    ) 
    } 
}); 

すべてのヘルプは素晴らしいことです。

+0

console.log(this.state)親の中に私に教えてください。 –

+0

コンソール上で "data Object {id =" - 559674239450219239 "、key =" SW-1014 "、name ="古いリリースを隠す "、more ...}" – Karthigeyan

+0

他のスタック回答を使用して、 TypeErrorを取得します。this.props.releasedataは未定義です。 – Karthigeyan

答えて

0

子コンポーネント内のcomponentWillRecievePropsイベントを使用して状態を更新できます(componentWillRecieveProps(nextProps)) 親コンポーネント内の子プロップを更新すると起動します。オブジェクトのプロパティをレンダリングする


ドキュメントhttps://facebook.github.io/react/docs/component-specs.html

+0

componentWillReceivePropsコメント。名前 \t \t}); \t \t console.log(name); \t} ==> TypeError:this.props.releasedataは定義されていません – Karthigeyan

+0

componentWillReceiveProps関数で設定しなければならない小道具の構文を教えてください。 – Karthigeyan

+0

componentWillReceiveProps:function(nextProps){console.log(nextProps); }コンソールからのメッセージを書いてください(what in nextProps) –

0

親コンポーネント:

componentDidMount: function(){ 
    return { data: [], 
      release: []};   ===> Defined the undefined variable type 
    }, 
    $.ajax({ 
     url: "http://localhost:8080/app/restapi/getreleasedata/"+Key+"?access_token="+accesstokenvalue+"" 
    }).then(function(data) { 
     this.setState({data: data, 
       release:data.releasedata ===> Assign directly object from JSON to 
               local state variable Defined 
      }); 
     }.bind(this)) 
    }, 
    render: function(){ 
    return (
     <PbiHeader pbiRelease={this.state.release.name} /> ==>Attach attribute 
        mapping inside object properties here "name" here we can add more 
        than one attribute eg: add release "id" "<PbiHeader pbiRelease={this.state.release.name} 
        pbiReleaseId={this.state.release.id} .... etc />" 
      ) 
    } 
    }); 

子コンポーネント:

var PbiHeader = React.createClass({ 
render: function(){ 
return (
       <tbody> 
       <tr> 
        <td>{this.props.pbiRelease}</td> //Here i mapped props to parent 
                attribute name 
       </tr> 
       </tbody> 
     ) 
    } 
    }); 

これは私がのプロパティにマップすることができる方法である私は、親SETSTATE内のすべてのプロパティ()自体を設定しますJSONのネストされたオブジェクト。他の方法がある場合は、追加してください。

関連する問題