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 / 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
@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-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 / 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 = () => {
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 / 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...
@danielkcz
danielkcz / SettingsProvider.jsx
Last active February 14, 2019 20:49
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
@danielkcz
danielkcz / RandomGiphy.jsx
Last active February 19, 2019 18:42
useQuery reusable
import { observer } from 'mobx-react-lite'
const RandomGiphy = observer(() => {
const settings = useSettings()
const giphy = useRandomGiphy(settings.tag)
return giphy ? <img src={giphy.url} /> : null
})
@danielkcz
danielkcz / machine.js
Last active November 12, 2019 17:29
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@danielkcz
danielkcz / machine.js
Created November 12, 2019 18:41
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions