Created
January 6, 2019 10:38
-
-
Save jeanbauer/f31f690228a3fc4cda101ae445d4eae2 to your computer and use it in GitHub Desktop.
Invariant Violation: Hooks can only be called inside the body of a function component.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Invariant Violation: Hooks can only be called inside the body of a function component. | |
2 | | |
3 | export function useFormField(initialValue) { | |
> 4 | const [value, setValue] = useState(initialValue) | |
| ^ | |
5 | | |
6 | function onChange(e) { | |
7 | setValue(e.target.value) | |
at invariant (node_modules/react/cjs/react.development.js:125:15) | |
at resolveDispatcher (node_modules/react/cjs/react.development.js:1424:28) | |
at useState (node_modules/react/cjs/react.development.js:1447:20) | |
at useFormField (components/hooks/hooks.js:4:29) | |
at Object.<anonymous> (components/hooks/__tests__/hooks-spec.js:8:20) | |
Dep: | |
``` | |
"next": "latest", | |
"react": "16.7.0-alpha.2", | |
"react-dom": "16.7.0-alpha.2", | |
"react-testing-library": "^5.4.2" | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from 'react' | |
export function useFormField(initialValue) { | |
const [value, setValue] = useState(initialValue) | |
function onChange(e) { | |
setValue(e.target.value) | |
} | |
return { | |
value, | |
onChange, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { render, fireEvent } from 'react-testing-library' | |
import { useFormField } from '../hooks' | |
describe('Hooks', () => { | |
describe('useFormField with input', () => { | |
it.skip('should set value and listen to type event', () => { | |
const { container } = render( | |
<input {...useFormField('')} /> | |
) | |
const input = container.firstChild | |
fireEvent.change(input, {target: {value: 'abc'}}) | |
expect(input.value).toBe('abc') | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment