Javascript Meteor.call并将对象传递给渲染组件
我正在使用Meteor从服务器获取数据,并根据某些GET参数进行渲染。我的URL是Javascript Meteor.call并将对象传递给渲染组件,javascript,reactjs,meteor,react-router,Javascript,Reactjs,Meteor,React Router,我正在使用Meteor从服务器获取数据,并根据某些GET参数进行渲染。我的URL是/course/:subject/:number;使用React Router,我可以正确获取参数,并使用Meteor.call函数从Meteor获取数据(这也可以正常工作。我可以获取我正在查找的数据。)Meteor返回一个我希望传递到React渲染的组件中的对象 但是,调用render方法时,this.state.thisClass为null。如果我使用componentWillMount而不是component
/course/:subject/:number
;使用React Router,我可以正确获取参数,并使用Meteor.call
函数从Meteor获取数据(这也可以正常工作。我可以获取我正在查找的数据。)Meteor返回一个我希望传递到React渲染的组件中的对象
但是,调用render
方法时,this.state.thisClass
为null
。如果我使用componentWillMount
而不是componentDidMount
,render
会被调用两次:一次调用的过程为null,这会导致错误;另一次调用的过程没有错误,并且使用正确的过程(不过,由于有错误,页面只是一个白色屏幕)
我是否误解了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 } />;
}
}
import React,{Component,PropTypes}来自'React';
从“流星/流星”中导入{Meteor};
从“/CourseCard.jsx”导入CourseCard;
//Permalink component—在数据库中搜索CourseCard后渲染它的组件
导出默认类Permalink扩展组件{
建造师(道具){
超级(道具);
常量编号=this.props.match.params.number;
const subject=this.props.match.params.subject.toLowerCase();
此.state={
号码:号码,,
主题:主题,,
此类:null
};
}
组件安装(){
Meteor.call(“getCourseByInfo”,this.state.number,this.state.subject,(err,foundClass)=>{
if(!err&&foundClass){
这是我的国家({
此类:foundClass
});
}
否则{
// 404
}
});
}
渲染(){
返回;
}
}
当this.state.thisClass为null或空时,不呈现CourseCard如何
render () {
return (
<div>
{
this.state.thisClass ? &&
<CourseCard course={ this.state.thisClass } />
}
</div>
);
}
render(){
返回(
{
这个,州,这个班&&
}
);
}
当this.state.thisClass为null或空时,不呈现CourseCard如何
render () {
return (
<div>
{
this.state.thisClass ? &&
<CourseCard course={ this.state.thisClass } />
}
</div>
);
}
render(){
返回(
{
这个,州,这个班&&
}
);
}
很抱歉,在您的渲染方法中似乎没有任何selectedClass
的实例,您的意思是thisClass
?是的,这就是我的意思。谢谢你抓住了!那么追溯一下,您可以注销什么是foundClass
?我认为问题在于回调和setState callOk之间,看起来根本没有调用componentDidMount
,因为我期望的项目不在那里,所以组件从未装入。我应该早点发现的。将其更改为componentWillMount
会导致render
发生两次;第一个位置thisClass
为空,导致错误,第二个位置thisClass
等于我要查找的内容。如果(this.state.thisClass),则始终可以在其周围添加一个条件。很抱歉,在渲染方法中似乎没有任何selectedClass
,你是说这门课吗?是的,我就是这个意思。谢谢你抓住了!那么追溯一下,您可以注销什么是foundClass
?我认为问题在于回调和setState callOk之间,看起来根本没有调用componentDidMount
,因为我期望的项目不在那里,所以组件从未装入。我应该早点发现的。将其更改为componentWillMount
会导致render
发生两次;第一个wherethisClass
为空,导致错误,第二个wherethisClass
等于我要查找的内容。如果(this.state.thisClass),您可以始终在其周围添加一个条件?