Skip to content

Instantly share code, notes, and snippets.

@joeynimu
Last active May 10, 2018 15:25
Show Gist options
  • Save joeynimu/f8adb510928fc8163aae9014e6b4abba to your computer and use it in GitHub Desktop.
Save joeynimu/f8adb510928fc8163aae9014e6b4abba to your computer and use it in GitHub Desktop.

Revisions

  1. joeynimu revised this gist May 10, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ComponentFile.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ const Filters = () => {
    <h3>Some data</h3>
    <ul>{
    regions && regions.map((d, i) => {
    return <li key={d.region_id}>d.region_name</li>
    return <li key={d.region_id}>{d.region_name}</li>
    })
    }</ul>
    </div>)
  2. joeynimu revised this gist May 9, 2018. No changes.
  3. joeynimu created this gist May 9, 2018.
    29 changes: 29 additions & 0 deletions ComponentFile.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import React from 'react'

    const Filters = () => {
    // this throws the error yet on inspecting with react tools I can see teh data `prop` with `regions` on there
    const regions = this.props.data.regions
    return (<div>
    <h3>Some data</h3>
    <ul>{
    regions && regions.map((d, i) => {
    return <li key={d.region_id}>d.region_name</li>
    })
    }</ul>
    </div>)
    }

    const areasQuery = `
    query{
    regions{
    region_name
    region_id areas{
    area_id collection_centers {
    collection_center_name
    }
    }
    }
    }
    `

    export default DataProvider(Filters, areasQuery)
    24 changes: 24 additions & 0 deletions DataProvider.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import React, { Component } from 'react'
    import { Provider, Client, query, Connect } from 'urql'
    import { GRAPHQL_ENDPOINT } from '../constants'

    const client = new Client({
    url: GRAPHQL_ENDPOINT
    })

    const DataProvider = (WrappedComponent, q) => {
    return class extends Component {
    render () {
    return (<Provider client={client}>
    <Connect
    query={query(q)}
    children={({ loaded, fetching, refetch, data, error }) => {
    return (<WrappedComponent data={data} {...this.props} />)
    }}
    />
    </Provider>)
    }
    }
    }

    export default DataProvider