Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Solution part 1: useContext

The Provider is interchangeable as class-based or function-based?

Less of a question more of an observation. I was so far off base with this challenge as I had in my head that I would need to change the Provider and its content to a Hooks, function-based set-up. Also, it became obvious to me that accessing Context via destructuring with the exercises threw me off what we are actually passing around when I saw your solution. I played with the solution until it had a closer similarity to the last examples with ScoreboardContext.

import React, { useContext } from 'react';
import { Context } from '../Context'; // destructured version

export default () => {
  // let context = useContext(Context.Context);
  // const authUser = context.authenticatedUser;
  const { authenticatedUser } = useContext(Context);
  return (
    <div className='bounds'>
      <div className='grid-100'>
        <h1>{authenticatedUser.name} is authenticated!</h1>
        <p>Your username is {authenticatedUser.username}.</p>
      </div>
    </div>
  );
};

[disclaimer I don't think this is more right, it just helped me realise it was the same result]

I'm pleased you have these exercises as I thought I had a reasonably strong grip on this topic but the size of this App mixed with the flashbacks and PTSD from the React Project 7 made me realise how thin my knowledge is about Context and Providers in general.

So my key takeaway is that the App was written in class-based form, the Provider is being exported as an object so this means we can refactor a component to use Hooks and let the other components continue to use the higher-order functions without issue.

Nick Huemmer
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Nick Huemmer
Front End Web Development Techdegree Graduate 26,840 Points

Thanks for this comment - it's good to see that I'm not the only one who both struggled to follow this exercise and used destructuring assignment for the both the import and authenticatedUser. At least it seems like that's how it would have been done if one were following the same patterns as the previous React Hooks module.

It's also a bit of bummer to have to go and dig back in and understand the Context API after so much time away from React since it was two projects ago and we weren't required to use it.