themeteorchef / base

A starting point for Meteor apps.
http://themeteorchef.com/base
MIT License
689 stars 253 forks source link

Passing Meteor.user() to a component? #252

Closed jenlikescode closed 7 years ago

jenlikescode commented 7 years ago

First, thank you for creating Base. It's been really helpful as I move from the Meteor/Blaze to Meteor/React world.

It's straight forward to pass subscription data to components via containers. But, trying to create a basic profile page has been surprisingly hard and I feel like I'm missing something simple. So far I've managed to pass the user object as a json string.

My question is- what is the best way to pass a logged in user's info to a react component?

In my container, I have...

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

And my component...

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;
themeteorchef commented 7 years ago

It looks like you were able to figure this out okay?

If not, you can pass the result of Meteor.user() directly as a prop without the need to do a JSON.stringify(); React will interpret it as an object and you can use it like user.emails[0].address, etc.

Adrianpn commented 7 years ago

What was the answer to this question? I am also trying to create a profile page but having trouble. Can this be something that can be added to a future base update? Thanks