2017-02-08 7 views
3

I持って反応させコンポーネント投稿:I「は反応-komposerから流星リアクトKomposer:レディ加入後にコンポーネントのコンストラクタを実行

import { composeWithTracker } from 'react-komposer'; 

インポート{composeWithTracker}で構成

export class Post extends React.Component { 
    constructor(props) { 
    this.state = this.props; 
    } 
    ... 
} 

、 '; インポート投稿 '../components/post.jsx';

function composer(props, onData) { 
    const subscription = Meteor.subscribe('post', props.postId); 

    if (subscription.ready()) { 
    const data = { 
     ready: true, 
     posts: Posts.findOne(props.postId).fetch() 
    } 
    onData(null, data); 
    } else { 
    onData(null, {ready: false}); 
    } 
} 

export default composeWithTracker(composer)(Post); 

投稿コンポーネント私はコンポーネントの状態にいくつかのプロパティを入れたいですが、からデータを取得する前にコンストラクタが実行されます!

データが送信されるまでどのように待ってから、propsstateに入れますか?

これは何ですか?リアクションコンコードはすべきですか?私はバージョン1.~を使用してcomposeWithTrackerを取得しています。

答えて

4

componentWillReceivePropsを使用して新しいプロパティを取得し、コンポーネントの状態として設定できます。この関数は、新しいプロパティがコンポーネントに渡されたときに実行されます。

export class Post extends React.Component { 
    // ... 
    componentWillReceiveProps(nextProps) { 
    this.setState({ 
     ...nextProps, 
    }); 
    } 
    // ... 
} 
+0

これはまさに私が探していたものでした。 – Knowledge

関連する問題