简体   繁体   English

材质ui Select和“ redux-form”的意外行为

[英]Unexpected behavior with material ui Select and 'redux-form'

I have a simple 'redux form' with a Select component from newest material-ui-next. 我有一个简单的“ redux表单”,其中包含来自最新 material-ui-next的Select组件。

import { TextField } from 'material-ui';

<Field
   name="name"
   component={TextField}
   select
>
   <MenuItem value={1}>Lily</MenuItem>
   <MenuItem value={2}>Mark</MenuItem>
</Field>

Works fine. 工作正常。 Hovewer , if I change the value prop from typeof number to string , eg Hovewer ,如果我将prop的value从typeof number更改为string ,例如

<Field
   name="name"
   component={TextField}
   select
>
   <MenuItem value="lily">Lily</MenuItem>
   <MenuItem value="mark">Mark</MenuItem>
</Field>

the value changes properly, but just after one second, the value becomes 0 (as it was initially), and the selected value disappears (it's empty from now on). 值会正确更改, 但是一秒钟后,该将变为0 (与最初一样),并且所选值消失 (此后为空)。 It had a correct value just for a moment, but somehow it's being automatically set back to 0 . 它只是暂时有一个正确的值,但是不知何故它会自动设置回0

Even tried with rendering the field: 甚至尝试渲染字段:

const renderSelectField = ({ input, label, meta: { touched, error }, children, ...custom }) => (
  <TextField
    {...input}
    select
    onChange={(event, index, value) => input.onChange(event.target.value)}
    children={children}
    {...custom}
  />
)

Still, it changes the value, and just after that it returns to 0 . 仍然会更改该值,然后立即返回0 If I console.log the form values, it shows up (after manually changing the value): 如果我用console.log表单值,它将显示出来(在手动更改值之后):

{ name: "Lily" }
{ name: 0 }
{ name: 0 }

(it happens in period of one second) (一秒钟内发生)

Looking forward for any help. 期待任何帮助。 Thank you. 谢谢。

Edit: This is what happens in redux dev tools, when choosing an item with string value - in this case pln . 编辑:这是在redux开发工具中发生的事情,当选择具有string值的项目时-在这种情况下为pln

在此处输入图片说明

基于此react-select问题redux-form问题 ,似乎您需要覆盖默认的onBlur事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM