redux-form-material-ui v5.0.0-beta.2中Select组件onChange报错求助
Fixing "Cannot read property 'onChange' of undefined" with redux-form-material-ui v5.0.0-beta.2 Select
I’ve run into this exact issue with the beta version of redux-form-material-ui paired with Material-UI v1, so let’s break down what’s going wrong and how to fix it.
Why You’re Seeing This Error
The core problem has two key parts:
- Mismatched onChange signature: Material-UI v1’s
Selectuses a differentonChangesignature than the oldSelectField— it takes(event, newValue)instead of(event, index, value). Using the old signature leads to undefined values breaking the flow. - Incorrect prop handling: When you pass a custom
onChangedirectly to theFieldcomponent or mishandle redux-form’sinputprops, you break the internal connection between the Select and redux-form’s state management.
Solution 1: Custom Render Component (Recommended)
This approach gives you full control while keeping redux-form’s state sync working correctly. Here’s the corrected code:
import { Select } from 'redux-form-material-ui'; import MenuItem from '@material-ui/core/MenuItem'; // Material-UI v1 MenuItem path const renderSelectField = ({ input, label, meta: { touched, error }, children, ...custom }) => { // Custom onChange: first update redux-form state, then run your logic const handleChange = (event, newValue) => { input.onChange(newValue); // Sync selected value with redux-form state // Add your custom onChange logic here (e.g., API calls, local state updates) console.log('Selected value:', newValue); }; return ( <Select label={label} error={touched && !!error} helperText={touched && error} {...input} onChange={handleChange} {...custom} > {children} </Select> ); }; // Usage in your form component const MaterialUiForm = props => { const { handleSubmit, pristine, reset, submitting } = props; return ( <form onSubmit={handleSubmit}> <div> <Field name="favoriteColor" component={renderSelectField} label="Favorite Color" > <MenuItem value="ff0000">Red</MenuItem> <MenuItem value="00ff00">Green</MenuItem> <MenuItem value="0000ff">Blue</MenuItem> </Field> </div> {/* Rest of your form elements */} <div> <button type="submit" disabled={submitting}>Submit</button> <button type="button" disabled={pristine || submitting} onClick={reset}>Reset</button> </div> </form> ); };
Solution 2: Direct Field Usage (Limited Use Cases)
If you don’t need a full custom component, you can use the Select directly with a wrapped onChange function that accesses redux-form’s input prop. Note this relies on internal React properties, so it’s less stable than the custom component approach:
<Field name="plan" component={Select} label="Select a plan" onChange={(event, newValue) => { // Access redux-form's input prop through the component's internal properties event.target.closest('[data-reactroot]')._reactInternalFiber.return.pendingProps.input.onChange(newValue); // Add your custom logic here }} > <MenuItem value="monthly">Monthly</MenuItem> <MenuItem value="yearly">Yearly</MenuItem> <MenuItem value="lifetime">Lifetime</MenuItem> </Field>
Key Fixes to Remember
- Update the onChange signature: Use
(event, newValue)to match Material-UI v1’s Select component. - Never skip redux-form sync: Always call
input.onChange(newValue)in your custom handler to keep redux-form’s state in sync with the selected value. - Pass children correctly: Ensure
MenuItemelements are passed through to the Select component by includingchildrenin your destructured props.
内容的提问来源于stack exchange,提问作者Galivan
相关产品推荐
相关产品推荐

