Javascript 使用Meteor React将数据从容器传递到组件
在使用Meteor和React时,我完全无法将数据从容器传递到组件。我以为我刚刚从Meteor React教程中复制了代码并进行了一点定制,但不知怎的它不起作用。我想做的是从数据库(QuestionModel)中获取数据,然后简单地在视图中输出。根据浏览器中的错误消息,renderQuestions()中的this.props.questions未定义。。。但是我显式地将数据传递给组件。 有人能帮我吗?多谢各位Javascript 使用Meteor React将数据从容器传递到组件,javascript,reactjs,meteor,Javascript,Reactjs,Meteor,在使用Meteor和React时,我完全无法将数据从容器传递到组件。我以为我刚刚从Meteor React教程中复制了代码并进行了一点定制,但不知怎的它不起作用。我想做的是从数据库(QuestionModel)中获取数据,然后简单地在视图中输出。根据浏览器中的错误消息,renderQuestions()中的this.props.questions未定义。。。但是我显式地将数据传递给组件。 有人能帮我吗?多谢各位 import React, { Component, PropTypes } fro
import React, { Component, PropTypes } from 'react';
import { createContainer } from 'meteor/react-meteor-data';
import { Questions as QuestionsModel } from '../../api/questions.js';
import { QuestionList } from '../QuestionList.jsx';
class Questions extends Component{
renderQuestions(){
let filteredQuestions = this.props.questions;
//This doesn't work at all...
console.log(this.props.questions);
return filteredQuestions.map((question) => (
<QuestionList key={question._id} question={question} />
));
}
render(){
return (
<div className="questionlist">
<ul>{this.renderQuestions()}</ul>
</div>
);
}
}
Questions.propTypes = {
questions: PropTypes.array.isRequired,
};
export default QuestionsContainer = createContainer(() => {
//This console.log outputs the data correctly.
console.log(QuestionsModel.find({}).fetch());
const questions = QuestionsModel.find({}).fetch();
return {
questions,
};
}, Questions);
import React,{Component,PropTypes}来自'React';
从“meteor/react meteor数据”导入{createContainer};
从“../api/Questions.js”导入{Questions as QuestionsModel};
从“../QuestionList.jsx”导入{QuestionList};
类问题扩展组件{
renderQuestions(){
让filteredQuestions=this.props.questions;
//这根本不起作用。。。
console.log(this.props.questions);
返回filteredQuestions.map((问题)=>(
));
}
render(){
返回(
{this.renderQuestions()}
);
}
}
问题.propTypes={
问题:需要PropTypes.array.isRequired,
};
导出默认问题容器=createContainer(()=>{
//此console.log正确输出数据。
log(QuestionsModel.find({}.fetch());
const questions=QuestionsModel.find({}).fetch();
返回{
问题,
};
},问题);
问题列表组件的代码是什么
我刚刚用{question.question} 替换了它,它对我来说非常适合
还有:您是否导入/imports/api/questions.js'代码>服务器端
这是我的密码:
import React, { Component, PropTypes } from 'react';
import { createContainer } from 'meteor/react-meteor-data';
import { Questions as QuestionsModel } from '../api/questions.js';
// import { QuestionList } from '../QuestionList.jsx';
class Questions extends Component{
renderQuestions(){
let filteredQuestions = this.props.questions;
//This doesn't work at all...
console.log(this.props.questions);
return filteredQuestions.map((question) => (
<li key={question._id}>{question.question}</li>
));
}
render(){
return (
<div className="questionlist">
<ul>{this.renderQuestions()}</ul>
</div>
);
}
}
Questions.propTypes = {
questions: PropTypes.array.isRequired,
};
export default QuestionsContainer = createContainer(() => {
//This console.log outputs the data correctly.
console.log(QuestionsModel.find().fetch());
const questions = QuestionsModel.find({}).fetch();
return {
questions,
};
}, Questions);
import React,{Component,PropTypes}来自'React';
从“meteor/react meteor数据”导入{createContainer};
从“../api/Questions.js”导入{Questions as QuestionsModel};
//从“../QuestionList.jsx”导入{QuestionList};
类问题扩展组件{
renderQuestions(){
让filteredQuestions=this.props.questions;
//这根本不起作用。。。
console.log(this.props.questions);
返回filteredQuestions.map((问题)=>(
{question.question}
));
}
render(){
返回(
{this.renderQuestions()}
);
}
}
问题.propTypes={
问题:需要PropTypes.array.isRequired,
};
导出默认问题容器=createContainer(()=>{
//此console.log正确输出数据。
log(QuestionsModel.find().fetch());
const questions=QuestionsModel.find({}).fetch();
返回{
问题,
};
},问题);
Retun应该是有效的DOM元素。这里您返回的问题
不是有效的DOM元素。谢谢您的回复。根据官方教程,容器可以返回类似数组的变量。这不是渲染任何内容,只是将数据传递给预期组件。奇怪的是无法从组件访问数据。非常感谢。我可能为问题列表设置了错误的路径,或者问题列表本身有错误。不管怎样,它现在起作用了。再次感谢。