Skip to content

Instantly share code, notes, and snippets.

@danielkcz
Last active February 14, 2019 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkcz/9921cd5e80cad0ec8fa91d09c1028ac2 to your computer and use it in GitHub Desktop.
Save danielkcz/9921cd5e80cad0ec8fa91d09c1028ac2 to your computer and use it in GitHub Desktop.
useQuery with MobX
import React from 'react'
const context = React.createContext(mobx.observable({
tag: 'kittens'
}))
export const SettingsProvider = ({ children }) => {
// we can employ some persistence here and load previous settings
// for now just return children because context already has state set
return children
}
// hook to grab observable state
export const useSettings = () => {
return React.useContext(context)
}
import { useQuery } from 'react-apollo-hooks'
import { observer } from 'mobx-react-lite'
const RandomGiphy = observer(() => {
const { tag } = useSettings()
const { data } = useQuery(
RandomGiphyQuery, {
variables: { tag }
}
}
// handle loading & error states...
return <img src={data.giphy.random.url} />
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment