formik: "Cannot read property 'type' of undefined" with react-select
Current Behavior
While using with react-select, changing the select results “Uncaught TypeError: Cannot read property ‘type’ of undefined”
Steps to Reproduce
<Formik
initialValues={{
assignedTo: task.assignedTo,
}}
onSubmit={(values) => {
const updatedTask = { ...task, ...values };
editTask(updatedTask);
}}
render={({ handleSubmit, handleChange, values }) => (
<form>
<Select type="text" name="assignedTo" options={options} onChange={handleChange} value={{ value: task.assignedTo, label: task.assignedTo }} />
</form>
})
/>
- Formik Version: 1.0.1
- React Version: 16.4.1
- TypeScript Version:
- Browser and Version: Chrome 67.0.3396.99
- OS: Mac OS High Sierra v10.13.5
- Node Version: 8.11.3
- Package Manager and Version: npm 5.6.0
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15
Here are the simple solution.
react-select
seems to callhandleChange
with something different than native DOMevent
. Use setFieldValue.Hi @prichodko, I’ve adapted the react-select example by Jared, although I’m implementing a custom input, I’m running into the same issue. I’ve set up handlers for the
onChange
andonBlur
events usingsetFieldValue
andsetFieldTouched
from theform
param.Debugging the formik handle change event I can see the
eventOrPath
object is just my value object, so I’m definitely handling this wrongly.What would be the correct way of implementing this?
@athenawisdoms here:
You could also forward your own
React.ChangeEvent
, although it feels very hacky:EDIT This is much tidier:
Mine looks like this:
Stacktrace:
formik.esm.js:763 Uncaught TypeError: Cannot read property ‘type’ of undefined at formik.esm.js:763 at formik.esm.js:797 at Object.<anonymous> (formik.esm.js:1302) at StateManager.callProp (stateManager-04f734a2.browser.esm.js:130) at StateManager._this.onChange (stateManager-04f734a2.browser.esm.js:70) at Select._this.onChange (Select-9fdb8cd0.browser.esm.js:1226) at Select._this.setValue (Select-9fdb8cd0.browser.esm.js:1253) at Select._this.selectOption (Select-9fdb8cd0.browser.esm.js:1302) at onSelect (Select-9fdb8cd0.browser.esm.js:1879) at HTMLUnknownElement.callCallback (react-dom.development.js:189) at Object.invokeGuardedCallbackDev (react-dom.development.js:238) at invokeGuardedCallback (react-dom.development.js:291) at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:306) at executeDispatch (react-dom.development.js:391) at executeDispatchesInOrder (react-dom.development.js:416) at executeDispatchesAndRelease (react-dom.development.js:3301) at executeDispatchesAndReleaseTopLevel (react-dom.development.js:3310)
@hugofovargue you override your custom
onChange
prop with field spreading{...field}
(field includesonChange
too).You can either specify your
onChange
after object spread or, what I would recommend, extracting it from field for the sake of explicitness (which is always a better thing to do).I have same problem with implement Select from Ant Design “formik”: “^1.0.0-beta.10”
Console error: formik.esm.js:241 Uncaught TypeError: Cannot read property ‘type’ of undefined
UPDATE: I replaced the onSelect event with onChange and everything became ok