Meteor.call 并将对象传递给呈现的组件
Meteor.call and passing object to a rendered component
我正在使用 Meteor 从服务器获取数据并根据某些 GET 参数呈现它。我的 URL 是 /course/:subject/:number
;使用 React Router,我正在正确获取参数并使用 Meteor.call
函数从 Meteor 获取数据(这也很好用。我得到了我正在寻找的数据。) Meteor returns我想传递给将由 React 呈现的组件的对象。
但是,调用render
方法时,this.state.thisClass
是null
。如果我使用 componentWillMount
而不是 componentDidMount
,render
会被调用两次:一次是课程为空,这会导致错误,另一次没有错误并且课程正确(尽管,由于出现错误,该页面只是一个白屏。)
我是不是误解了 componentWillMount
和 componentDidMount
的功能?我应该做点别的吗?
import React, { Component, PropTypes } from 'react';
import { Meteor } from "meteor/meteor";
import CourseCard from './CourseCard.jsx';
// Permalink component - Component to render a CourseCard after searching for it in the database
export default class Permalink extends Component {
constructor (props) {
super(props);
const number = this.props.match.params.number;
const subject = this.props.match.params.subject.toLowerCase();
this.state = {
number: number,
subject: subject,
thisClass: null
};
}
componentDidMount () {
Meteor.call("getCourseByInfo", this.state.number, this.state.subject, (err, foundClass) => {
if (!err && foundClass) {
this.setState({
thisClass: foundClass
});
}
else {
// 404
}
});
}
render () {
return <CourseCard course={ this.state.thisClass } />;
}
}
当 this.state.thisClass 为 null 或为空时不呈现 CourseCard 怎么样?
render () {
return (
<div>
{
this.state.thisClass ? &&
<CourseCard course={ this.state.thisClass } />
}
</div>
);
}
我正在使用 Meteor 从服务器获取数据并根据某些 GET 参数呈现它。我的 URL 是 /course/:subject/:number
;使用 React Router,我正在正确获取参数并使用 Meteor.call
函数从 Meteor 获取数据(这也很好用。我得到了我正在寻找的数据。) Meteor returns我想传递给将由 React 呈现的组件的对象。
但是,调用render
方法时,this.state.thisClass
是null
。如果我使用 componentWillMount
而不是 componentDidMount
,render
会被调用两次:一次是课程为空,这会导致错误,另一次没有错误并且课程正确(尽管,由于出现错误,该页面只是一个白屏。)
我是不是误解了 componentWillMount
和 componentDidMount
的功能?我应该做点别的吗?
import React, { Component, PropTypes } from 'react';
import { Meteor } from "meteor/meteor";
import CourseCard from './CourseCard.jsx';
// Permalink component - Component to render a CourseCard after searching for it in the database
export default class Permalink extends Component {
constructor (props) {
super(props);
const number = this.props.match.params.number;
const subject = this.props.match.params.subject.toLowerCase();
this.state = {
number: number,
subject: subject,
thisClass: null
};
}
componentDidMount () {
Meteor.call("getCourseByInfo", this.state.number, this.state.subject, (err, foundClass) => {
if (!err && foundClass) {
this.setState({
thisClass: foundClass
});
}
else {
// 404
}
});
}
render () {
return <CourseCard course={ this.state.thisClass } />;
}
}
当 this.state.thisClass 为 null 或为空时不呈现 CourseCard 怎么样?
render () {
return (
<div>
{
this.state.thisClass ? &&
<CourseCard course={ this.state.thisClass } />
}
</div>
);
}