如何使用流星文档对象从容器反应成分?(How to use a Meteor document ob

2019-10-29 12:05发布

我只是想从容器中的对象文件传递给我的组件,并使用它。 容器的代码是这样的:

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>
...

但是,我得到这个错误: 类型错误:无法读取属性“农布雷”的不确定问题是行:

//THE PROBLEM HERE!!!!!
value = {this.props.oneProject.nombre}

Answer 1:

一旦事情已经加载这将正常工作。 你需要返回isLoading从容器组件。

我也建议使用oneProject本身,因为你在任何情况下渲染的顶部得到它:

const { oneProject, isLoading } = this.props;



文章来源: How to use a Meteor document object from container to react component?