How to use a Meteor document object from container to react component?

安稳与你 提交于 2019-12-12 05:36:38

问题


I just want to pass an object document from the Container to my component and use it. The code of container is this:

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);

And i use it in my presentational component on this way:

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

But i get this error: TypeError: Cannot read property 'nombre' of undefined The problem is line:

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

回答1:


This will work fine once things have loaded. You need to return isLoading from the container component.

I would also recommend using oneProject by itself since you get it at the top of render in any case:

const { oneProject, isLoading } = this.props;



来源:https://stackoverflow.com/questions/46746047/how-to-use-a-meteor-document-object-from-container-to-react-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!