我只想将一个对象文档从Container传递给我的组件并使用它。容器的代码是:
import { Meteor } from 'meteor/meteor';
import { withTracker } from 'meteor/react-meteor-data';
import { Projects } from '/imports/api/projects.js';
import ProjectFormUpdate from './ProjectFormUpdate.jsx';
export default ProjectFormUpdateContainer = withTracker(({ key1 }) => {
Tracker.autorun(() => {
const sub = Meteor.subscribe('projects');
if (sub.ready()){
const oneProject = Projects.findOne(key1);
console.log(oneProject.nombre);
}})
return {
oneProject,
};
})(ProjectFormUpdate);
我以这种方式在我的演示组件中使用它:
render() {
const { oneProject, isLoading } = this.props;
if (!isLoading)
return (
<div className="col-xs-11">
<div className="box box-solid">
<form className="form" onSubmit={this.handleSubmit.bind(this)} >
<div className="box-body">
<div className="row">
<div className="col-xs-2">
<input
className = "form-control input-sm"
type="text"
ref="codigoInput"
placeholder="Código del Proyecto"
//THE PROBLEM HERE!!!!!
value = {this.props.oneProject.nombre}
onChange = {this.handleUpdate.bind(this)}
/>
</div>
...
但我收到此错误: TypeError:无法读取属性&#39; nombre&#39;未定义的 问题是这一行:
//THE PROBLEM HERE!!!!!
value = {this.props.oneProject.nombre}
答案 0 :(得分:0)
一旦加载完毕,这将正常工作。您需要从容器组件返回isLoading
。
我还建议单独使用oneProject
,因为在任何情况下都将它放在渲染的顶部:
const { oneProject, isLoading } = this.props;