react-container-query / react-container-query

:package: Modular responsive component
http://react-container-query.github.io/react-container-query/
MIT License
890 stars 47 forks source link

Allow applyContainerQuery to work as a curried function #60

Open Pajn opened 7 years ago

Pajn commented 7 years ago

Most higher order components allows you to partially apply the options before applying the component, for example recompose, react-redux, react-apollo. This allows you to compose multiple higher order compose like this

export const enhance = compose(
  mapProps(({config}) => ({
    config,
    deviceId: config.deviceId,
    interfaceIds,
  })),
  graphql(gql`
    query($deviceId: String!, $interfaceIds: [String!]) {
      device(id: $deviceId) {
        id
        name
        interfaceIds
        status(interfaceIds: $interfaceIds) {
          id
          interfaceId
          statusId
          value
        }
      }
    }
  `),
  mapProps(props => ({
    ...props,
    status: props.data.device && asObject(props.data.device.status),
  })),
)

It would be nice if this library supported the same pattern

d6u commented 7 years ago

Thanks for your contribution. I'm on my family vacation now. Will look at this when I come back.

Best, Daiwei

On Jun 11, 2017, 5:50 PM +0800, Rasmus Eneman notifications@github.com, wrote:

Most higher order components allows you to partially apply the options before applying the component, for example recompose, react-redux, react-apollo. This allows you to compose multiple higher order compose like this export const enhance = compose( mapProps(({config}) => ({ config, deviceId: config.deviceId, interfaceIds, })), graphql(gql query($deviceId: String!, $interfaceIds: [String!]) { device(id: $deviceId) { id name interfaceIds status(interfaceIds: $interfaceIds) { id interfaceId statusId value } } } ), mapProps(props => ({ ...props, status: props.data.device && asObject(props.data.device.status), })), ) It would be nice if this library supported the same pattern — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

d6u commented 7 years ago

That's a good idea. We have to figure out a way to order the arguments, so they become useful. Might be tricky because we have an optional initialSize argument.

drewlustro commented 6 years ago

+1 on this... the style of applyContainerQuery is not compliant with best-practices design of HoC's. The signature should be something like:

applyContainerQuery(query) => (BaseComponent) => { ... containerQuery props decorated .... }

That way, this module could be used with recompose

d6u commented 6 years ago

My original thought on this is that I agree curried function is useful and popular, but I want to leave this choice to user. You can easily use Ramda or similar library to create your own curried version of applyContainerQuery, although it's likely to increase the bundle size a couple kb.

GeKorm commented 6 years ago

This is solved by using your own HOC over <ContainerQuery />, like this:

const customContainerQuery = (query) => (Component) => (props) => (
  <ContainerQuery query={query}>
    {(params) => <Component containerQuery={params} {...props} />}
  </ContainerQuery>
);

// Usage:
customContainerQuery(query)(MyComponent)

Works with recompose.

drewlustro commented 6 years ago

Dope, thanks.

On Wed, Dec 20, 2017 at 5:43 AM, George Kormaris notifications@github.com wrote:

This is solved by using your own HOC over , like this:

const customContainerQuery = (query) => (Component) => (props) => (

{(params) => }

); // Usage:customContainerQuery(query)(MyComponent)

Works with recompose.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/d6u/react-container-query/issues/60#issuecomment-353029431, or mute the thread https://github.com/notifications/unsubscribe-auth/AAL5RQAb98FKxqzIJByKKzIB3XcdeNFYks5tCOTogaJpZM4N2WKM .