【问题标题】:Passing Meteor.user() to a component?将 Meteor.user() 传递给组件?
【发布时间】:2017-04-25 03:29:48
【问题描述】:

我目前正在使用themeteorchef/base 样板项目。当我从 Meteor/Blaze 转到 Meteor/React 世界时,这真的很有帮助。

通过容器将订阅数据传递给组件非常简单。但是,尝试创建一个基本的个人资料页面非常困难,我觉得我错过了一些简单的东西。到目前为止,我已经设法将用户对象作为 json 字符串传递(这并不理想)。

我的问题是 - 将登录用户的信息传递给反应组件的最佳方式是什么?

在我的容器中,我有...

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;

【问题讨论】:

    标签: javascript reactjs meteor


    【解决方案1】:

    终于搞定了!

    通过容器传递 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;
    

    【讨论】:

      【解决方案2】:

      事实上,您可以在任何地方访问“Meteor.user()”,因此您不需要从 Composer 或 Parent-Component 传递它。 因此,在您的简单组件中,您可以使用:

      import React from 'react';
      import NotFound from './NotFound';
      
      const ViewProfile = () => { 
        return (Meteor.user()) ? (
          <p>{JSON.stringify(Meteor.user())}</p>
        ) : <NotFound />;
      };
      
      export default ViewProfile;
      

      【讨论】:

      • 感谢您的回复!目前进行此编辑后,Meteor.user 仍以 undefined 的身份返回。据我了解, Meteor.user() 是反应性的,因此在初始加载时它将是未定义的。我需要以某种方式检查用户集合的 .ready() 吗?
      • 似乎有两种情况返回 undefined for Meteor.user()
      • (对不起,我打错了)似乎有两种情况会为 Meteor.user() 返回 undefined:1 是用户没有登录,2 是用户刚刚登录之后,您的页面在 Meteor.user() 可以访问之前加载!实际上,像以前的版本一样使用 composer 是很常见的,也是处理这个用户的好方法。您自己的更新代码现在很好。
      • Meteor 文档建议最低限度地调用meteor.user(),因此有时最好将其作为道具传递给组件
      猜你喜欢
      • 2020-05-07
      • 2017-10-11
      • 2020-08-05
      • 2017-12-02
      • 2019-01-18
      • 2017-12-26
      • 2017-08-20
      • 2019-10-28
      • 2021-06-18
      相关资源
      最近更新 更多