Skip to content

Commit

Permalink
Refactor getContext back to a simple context file (final-form#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and erikras committed Jun 14, 2019
1 parent 3dc413a commit 9cd3343
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 27 deletions.
10 changes: 2 additions & 8 deletions src/FormSpy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@ import renderComponent from './renderComponent'
import type { FormSpyPropsWithForm as Props, FormSpyRenderProps } from './types'
import type { FormApi, FormValuesShape } from 'final-form'
import isSyntheticEvent from './isSyntheticEvent'
import useForm from './useForm'
import useFormState from './useFormState'
import getContext from './getContext'

function FormSpy<FormValues: FormValuesShape>({
onChange,
subscription,
...rest
}: Props<FormValues>) {
const ReactFinalFormContext = getContext<FormValues>()
const reactFinalForm: ?FormApi<FormValues> = React.useContext(
ReactFinalFormContext
)
if (!reactFinalForm) {
throw new Error('FormSpy must be used inside of a ReactFinalForm component')
}
const reactFinalForm = useForm<FormValues>('FormSpy')
const state = useFormState({ onChange, subscription })
if (onChange) {
return null
Expand Down
2 changes: 1 addition & 1 deletion src/FormSpy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('FormSpy', () => {
expect(errorSpy).toHaveBeenCalled()
expect(errorSpy).toHaveBeenCalledTimes(1)
expect(errorSpy.mock.calls[0][0].message).toBe(
'FormSpy must be used inside of a ReactFinalForm component'
'FormSpy must be used inside of a <Form> component'
)
console.error.mockRestore()
})
Expand Down
3 changes: 1 addition & 2 deletions src/ReactFinalForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import useConstant from './useConstant'
import shallowEqual from './shallowEqual'
import isSyntheticEvent from './isSyntheticEvent'
import type { FormRenderProps } from './types.js.flow'
import getContext from './getContext'
import ReactFinalFormContext from './context'
import useLatest from './useLatest'

export const version = '6.1.0'
Expand Down Expand Up @@ -53,7 +53,6 @@ function ReactFinalForm<FormValues: FormValuesShape>({
validateOnBlur,
...rest
}: Props<FormValues>) {
const ReactFinalFormContext = getContext<FormValues>()
const config: Config<FormValues> = {
debug,
destroyOnUnregister,
Expand Down
5 changes: 5 additions & 0 deletions src/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow
import * as React from 'react'
import type { FormApi } from 'final-form'

export default React.createContext<?FormApi<any>>()
14 changes: 0 additions & 14 deletions src/getContext.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/useForm.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// @flow
import * as React from 'react'
import type { FormApi, FormValuesShape } from 'final-form'
import getContext from './getContext'
import ReactFinalFormContext from './context'

function useForm<FormValues: FormValuesShape>(
componentName?: string
): FormApi<FormValues> {
const ReactFinalFormContext = getContext<FormValues>()
const form: ?FormApi<FormValues> = React.useContext(ReactFinalFormContext)
if (!form) {
throw new Error(
Expand Down

0 comments on commit 9cd3343

Please sign in to comment.