adamsoffer / next-apollo

React higher-order component for integrating Apollo Client with Next.js
MIT License
481 stars 64 forks source link

Make next-apollo a HOC #1

Closed kolpav closed 7 years ago

kolpav commented 7 years ago

Hi,

Nice library 👍 but I think there is room for improvement for api of withData also I don't think I would call it a HOC. From react documentation page

a higher-order component is a function that takes a component and returns a new component

But you are taking two parameters, config object and component as second parameter.

Why does it matter?

  1. Every HOC out there does configuration like this

    import HOCFactory from 'cool-hoc'
    const myProjectSpecificHOC = HOCFactory(config)
    export default myProjctSpecificHOC(props => ....)

    You can take a look at redux-form's reduxForm or react-redux's connect from top of my head. Basically if you need configuration for your HOC you make it HOC factory.

  2. You can't use compose like with every other HOC

    
    import { compose } from 'lodash' // or redux

const CoolComponent = props => ...

export default compose( connect(mapStateToProps, mapDispatchToProps), reduxForm({...}), withCurrentRoute, withData ??? <==== here is the problem )(CoolComponent)


3. In every component where you want to use `withData` you also need to import its config object instead of configuring it once and exporting it. You could then have `import { myProjectSpecificWithData } from './hocs'` which is much nicer.

## TL;DR;

Instead of `withData(config, component)` do `withData(config)(component)`
adamsoffer commented 7 years ago

Great points. Thanks for the feedback! I'll make another release with these changes.

adamsoffer commented 7 years ago

@kolpav I just published a new release branch for review. Check it out here.

Now inside a Next.js page you can use withData like so:

const Home = props => (
  <Main>
    <Header pathname={props.url.pathname} />
    <Submit />
    <PostList />
  </Main>
)

export default withData(config)(Home)

Is this what you envisioned?

adamsoffer commented 7 years ago

All set! Going to close. Let me know if you have any further feedback.