Repository navigation
Description
Made withAuth to not have to mess with <App> and give example of making HOC. Not feeling like either reason is very important.
Globals?
Inspired by DHH's series on writing software well, in an episode recommending judicious use of globals, eg for the current user.
Could be used for login/logout functions.
- Pro: Easy
- Con: Testing—for various forms of testing (including explicit unit tests and things like storybook), you then have to mock out the globals.
// CurrentUserQuery.js
export let login, logout
class CurrentUserQuery extends Component
constructor() {
login = this.login
logout = this.logout
}
login = () =>
logout = () => So that eg in Profile.js you can:
import { login, logout } from './CurrentUserQuery'instead of getting them as props.
Prop drilling vs HOC vs Context/unstated
Need a reactive data source for loading & currentUser, so can't do globals.
Reusable query
Could do a withUser() HOC that provides them through graphql() to any component that needs it. All using default cache-first, and deduplication should combine them into one query, while also maintaining data.loading/loadingUser.
- Pro: concise. Using Apollo store like the global variable it is
- Con: how to incorporate
loggingInintoloading? In the single-usewithAuth, it's defined as:loading={loading || this.state.loggingIn}
class Profile extends Component {
...
}
export default withUser(Profile)(or alternatively could make a reusable <WithUser> query component, but that takes more characters and indentation to use)
Prop drilling
A single normal query at the top. Lot of drilling. It's nice for testing, but not nice to read/write. Would personally prefer to avoid.
<CurrentUserQuery>
{({ loading, error, data }) => (
<App loadingUser={loading} currentUser={data.currentUser} />
)}
</CurrentUserQuery>Context/unstated
- Pro: no drilling
- Con: verbose
Activity
unicodeveloper commentedon Jun 17, 2018
Hey @lorensr IMO, if you are using
withAuthonce, why make it at all? I'd rather not allow it to exist.One authentication service, like
Auth.jswith all the helpers needed can do the magic. Check this Auth Service file.And then comfortably bring in the
withUserHOC!lorensr commentedon Jun 17, 2018
unicodeveloper commentedon Jun 17, 2018
No, I don't. It's just a matter of trade-offs.
Do you want to prevent prop drilling because of how stressful and confusing it can be in the long run? If yes, then just use the
withUserHOC anywhere you need it.lorensr commentedon Aug 13, 2018
Okay, here's the plan! I'm liking it a lot. Lmk if you have any further thoughts @unicodeveloper @jeresig
loginInProcessstatus: apollo-link-statewithUser: cache-only HOCAnd then in components:
This simplifies
App.js/routing a lot and completely eliminates passing aroundlogin/logout/user/loggingIneverywhere.lorensr commentedon Aug 28, 2018
Turned out to be simpler—given deduplication and default
cache-firstfetch policy, there's no point to thefetchUserDataandfetchPolicy: 'cache-only'withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33
withAuth -> withUser, fixes #33