Skip to content

Instantly share code, notes, and snippets.

View danielkcz's full-sized avatar
🏗️
Building own experimental dating app

Daniel K. danielkcz

🏗️
Building own experimental dating app
View GitHub Profile
@danielkcz
danielkcz / RandomGiphy.jsx
Last active October 15, 2020 05:19
useQuery + useContext
import { useQuery } from 'react-apollo-hooks'
const RandomGiphy = () => {
const { tag } = useSettings()
const { data } = useQuery(
RandomGiphyQuery, {
variables: { tag }
}
}
// handle loading & error states...
import { useQuery } from 'react-apollo-hooks'
const RandomGiphy = ({ tag }) => {
const { data } = useQuery(
RandomGiphyQuery, {
variables: { tag }
}
}
// handle loading & error states...
return <img src={data.giphy.random.url} />
@danielkcz
danielkcz / mutations.jsx
Last active February 15, 2019 07:58
Mutations with Hooks
// HOC approach was first and some still prefer it for whatever reasons
// Along with recompose you can do some real nasty tricks
const MutateMe = graphql(MutationDocument, { name: 'execute' })(({ execute }) => {
// In comparison it's not a terrible way to execute a mutation
// But naming the mutation with string 😆
return <button onClick={() => execute({ variables: { secretName: 'Me' }})} />
})
// Mutation component is fine for simple cases, but gets ugly otherwise
const MutateMe = () => {
@danielkcz
danielkcz / formik-state.js
Last active February 13, 2019 13:36
Formik with MobX #3
const formik = mobx.observable({
values: props.initialValues || {},
touched: {},
errors: {},
// scalar values needs to be wrapped into another object
// otherwise after spreading MobX would lost track of it
state: {
isSubmitting: false,
submitError: null
}
@danielkcz
danielkcz / mobx.js
Last active December 27, 2019 09:11
Formik with MobX #2
React.useEffect(() =>
mobx.autorun(() => {
if (props.validate) {
// MobX will see use of formik.values and rerun this function whenever it is mutated
formik.errors = props.validate(formik.values);
}
}), []
);
@danielkcz
danielkcz / formik-mobx.js
Last active July 26, 2023 21:51
Formik with MobX
function useFormik(props) {
// useState to keep the same observable around without recreating it on each render
const [formik] = React.useState(() =>
mobx.observable({
values: props.initialValues || {},
touched: {}
})
)
// just mutate state, this function itself can be considered an action+reducer
import { Trans } from '@lingui/react'
import React, { Children } from 'react'
interface IProps {
children: ReactNode | string
}
interface IState {
refs: React.RefObject<Trans>[]
@danielkcz
danielkcz / buildMutation.ts
Last active March 7, 2018 13:25
Apollo GraphQL with render props
import { ApolloQueryResult } from 'apollo-client'
import { DocumentNode } from 'graphql'
import React from 'react'
import { graphql } from 'react-apollo'
import { MutationFunc, MutationOpts } from 'react-apollo/types'
interface IQueryProps<TResult, TVariables = {}> {
render(
execute: (variables?: Partial<TVariables>) => Promise<TResult>,
executionOptions?: MutationOpts<TVariables>,
@danielkcz
danielkcz / mobx-react-form.d.ts
Created March 1, 2018 08:49
Crude typescript defintion for MRF
declare module 'mobx-react-form' {
export default MRF.MobxReactForm
}
type Dictionary<T> = { [key: string]: T }
declare namespace MRF {
class MobxReactForm {
constructor(options: MRF.Options, settings?: MRF.Settings)
isValid: boolean
@danielkcz
danielkcz / TimingModel.ts
Created February 21, 2018 10:15
Timing with MST
import { types } from 'mobx-state-tree'
const SECOND = 1 * 1000
const MINUTE = 60 * SECOND
export const TimingModel = types
.model('Timing', {
bySecond: types.optional(types.number, Infinity),
byMinute: types.optional(types.number, Infinity),
autoStart: false,