Reactjs Meteor React Komposer:在准备好订阅后执行组件构造函数
我有一个React组件Post: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.
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,
});
}
// ...
}
那正是我要找的!