Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Meteor React Komposer:在准备好订阅后执行组件构造函数_Reactjs_Meteor - Fatal编程技术网

Reactjs Meteor React Komposer:在准备好订阅后执行组件构造函数

Reactjs Meteor React Komposer:在准备好订阅后执行组件构造函数,reactjs,meteor,Reactjs,Meteor,我有一个React组件Post: export class Post extends React.Component { constructor(props) { this.state = this.props; } ... } ,我用它作曲 import { composeWithTracker } from 'react-komposer'; 从'react komposer'导入{composeWithTracker}; 从“../components/Post.

我有一个React组件Post

export class Post extends React.Component {
  constructor(props) {
     this.state = this.props;
  }
  ...
}
,我用它作曲

import { composeWithTracker } from 'react-komposer';
从'react komposer'导入{composeWithTracker}; 从“../components/Post.jsx”导入Post

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);
。正如Post组件中所给出的,我想将一些属性置于组件的状态,但是在我从composer获取数据之前,构造函数将被执行

我如何等待数据发送,然后将我的
道具
置于
状态


这不正是总统应该做的吗?顺便说一句,我使用的是版本
1.~
来获取
ComponentWithTracker

您可以使用
componentWillReceiveProps
来获取新属性并设置为组件状态。每当有新属性传递给组件时,此函数将运行:

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

那正是我要找的!