arunoda / react-komposer

Feed data into React components by composing containers.
MIT License
732 stars 70 forks source link

Question: Passing Meteor.user() to a component? #162

Closed jenlikescode closed 7 years ago

jenlikescode commented 7 years ago

I'm currently using themeteorchef/base boilerplate project. 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 (which is not ideal).

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;