问题
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