Javascript 是否将Meteor.user()传递给组件?
我目前正在使用样板项目。当我从流星/火焰移动到流星/反应世界时,这真的很有帮助 通过容器将订阅数据传递给组件是很简单的。但是,试图创建一个基本的个人资料页面却异常困难,我觉得我错过了一些简单的东西。到目前为止,我已经设法将用户对象作为json字符串传递(这并不理想) 我的问题是-将登录用户的信息传递给react组件的最佳方式是什么 在我的容器里,我有Javascript 是否将Meteor.user()传递给组件?,javascript,reactjs,meteor,Javascript,Reactjs,Meteor,我目前正在使用样板项目。当我从流星/火焰移动到流星/反应世界时,这真的很有帮助 通过容器将订阅数据传递给组件是很简单的。但是,试图创建一个基本的个人资料页面却异常困难,我觉得我错过了一些简单的东西。到目前为止,我已经设法将用户对象作为json字符串传递(这并不理想) 我的问题是-将登录用户的信息传递给react组件的最佳方式是什么 在我的容器里,我有 import { Meteor } from 'meteor/meteor'; import { composeWithTracker } fro
import { Meteor } from 'meteor/meteor';
import { composeWithTracker } from 'react-komposer';
import ViewProfile from '../pages/ViewProfile.js';
import Loading from '../components/Loading.js';
const composer = ({ params }, onData) => {
const currentUser = JSON.stringify( Meteor.user() );
onData(null, { currentUser });
};
export default composeWithTracker(composer, Loading)(ViewProfile);
我的组件是一个简单的显示器
import React from 'react';
import NotFound from './NotFound';
const ViewProfile = ( { currentUser } ) => {
return currentUser ? (
<p>{ currentUser }</p>
) : <NotFound />;
};
ViewProfile.propTypes = {
currentUser: React.PropTypes.string
};
export default ViewProfile;
从“React”导入React;
从“/NotFound”导入NotFound;
const ViewProfile=({currentUser})=>{
返回当前用户(
{currentUser}
) : ;
};
ViewProfile.propTypes={
当前用户:React.PropTypes.string
};
导出默认视图配置文件;
事实上,您可以在任何地方访问“Meteor.user()”,因此不需要从Composer或父组件传递它。
因此,在简单组件中,您可以使用:
import React from 'react';
import NotFound from './NotFound';
const ViewProfile = () => {
return (Meteor.user()) ? (
<p>{JSON.stringify(Meteor.user())}</p>
) : <NotFound />;
};
export default ViewProfile;
从“React”导入React;
从“/NotFound”导入NotFound;
const ViewProfile=()=>{
返回(Meteor.user())(
{JSON.stringify(Meteor.user())}
) : ;
};
导出默认视图配置文件;
终于让它工作了
通过容器传递Meteor.user(反应数据)仍然是正确的方法,它可以到达组件,但是,在我的组件中,我只需要引用特定的对象值(字符串或数组)
所以在我的容器中:
import { composeWithTracker } from 'react-komposer';
import ViewProfile from '../pages/ViewProfile.js';
import Loading from '../components/Loading.js';
const composer = (params, onData) => {
const user = Meteor.user();
if (user) {
const currentUser = {
fname: user.profile.name.first,
lname: user.profile.name.last,
email: user.emails[0].address
}
onData(null, { currentUser });
}
export default composeWithTracker(composer, Loading)(ViewProfile);
然后在我的组件中:
import React from 'react';
import NotFound from './NotFound';
const ViewProfile = ( {currentUser} ) => {
//console.log(currentUser);
return (currentUser) ? (
<p>{ currentUser.fname }</p>
<p>{ currentUser.lname }</p>
<p>{ currentUser.email }</p>
) : <NotFound />;
};
ViewProfile.propTypes = {
currentUser: React.PropTypes.object,
};
export default ViewProfile;
从“React”导入React;
从“/NotFound”导入NotFound;
常量视图配置文件=({currentUser})=>{
//console.log(当前用户);
返回(当前用户)(
{currentUser.fname}
{currentUser.lname}
{currentUser.email}
) : ;
};
ViewProfile.propTypes={
当前用户:React.PropTypes.object,
};
导出默认视图配置文件;
谢谢您的回复!当前通过此编辑,Meteor.user仍以未定义的状态返回。据我所知,Meteor.user()是被动的,所以在初始加载时它是未定义的。我需要为users集合检查.ready()吗?似乎有两个案例返回Meteor.user()未定义(抱歉,我输入错误)似乎有两个案例返回Meteor.user()未定义:1是用户未登录,2是在用户刚刚登录之后,在访问Meteor.user()之前,您的页面已加载!实际上,在以前的版本中使用composer是很常见的,也是处理此用户的一种好方法。您自己的更新代码现在已经很好了。Meteor文档建议尽量少调用Meteor.user(),因此,有时最好将其作为道具传递给组件