简体   繁体   English

Material-ui 自动完成不会根据值属性更改其值

[英]Material-ui autocomplete not changing its value based on value prop

I had to generate multiple rows, where each row has a dropdown (Autocomplete) that was searchable through API, and other fields too.我必须生成多行,其中每一行都有一个下拉菜单(自动完成),可以通过 API 和其他字段进行搜索。 All is working perfectly, except when I delete a row from the middle somewhere, just the autocomplete component displays the same value as earlier, however, I get correct data in my state.一切都运行良好,除了当我从某处的中间删除一行时,只有自动完成组件显示与之前相同的值,但是,我在 state 中获得了正确的数据。

So if I had an array with the following items.因此,如果我有一个包含以下项目的数组。

在此处输入图像描述

And now if I delete Row 2 which is Brite – (500 g), I get this display on the screen.现在,如果我删除第 2 行,即 Brite – (500 g),我会在屏幕上看到这个显示。

在此处输入图像描述

However, all other items of the rows are correctly displayed, so I'm assuming there is nothing wrong with my logic of handling the array, but due to autocomplete showing wrong value(however it holds the correct object passed into its value prop), it seems that all items have suddenly changed their Qty and prices, and the last item was deleted (instead of 2nd one).但是,行的所有其他项目都正确显示,所以我假设我处理数组的逻辑没有问题,但是由于自动完成显示错误的值(但是它保存了正确的 object 传递到它的 value 属性),似乎所有商品的数量和价格都突然发生了变化,最后一个商品被删除(而不是第二个)。 If I sum up the problem, although the component was re-rendered based on new array in the state, that was obtained after deleting a row, autocomplete still displays its previous value which is nowhere is the data now.如果我总结这个问题,虽然该组件是基于state中的新数组重新渲染的,这是删除一行后获得的,但自动完成仍然显示它之前的值,而现在已经不是数据了。

在此处输入图像描述

<Autocomplete
                            options={skuItems}
                            getOptionLabel={option => option.name}
                            renderInput={params => (
                                <TextField {...params} label="SKU" variant="outlined" />
                            )}
                            value={selectedSkuItems[index]}
                            onChange={(e, val) => orderItemHandleChange(e, val, index)}
                            onInputChange={skuSearch}
/> (edited) 

The issue resides where you have the map function.问题出在您拥有 map function 的地方。 The key must be something unique.密钥必须是独一无二的。 If it involves the index, is bad.如果涉及到索引,那就不好了。 Because you remove items by index and then another item takes that indexes place, and a proper rewrite of the DOM is failing.因为您按索引删除项目,然后另一个项目取代该索引,并且正确重写 DOM 失败。 As seen bellow:如下图所示:

collection.map((value, index) => { <div key={something unique}> </div>}

暂无
暂无

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

相关问题 获取 React material-UI 自动完成中的值 - Getting the value in the React material-UI Autocomplete 在 Material-UI 自动完成中获取选定的值 - Getting selected value in Material-UI Autocomplete React Material-UI Autocomplete:如何在更改另一个字段中的值时清除在自动完成字段中选择的多个值(mui 芯片)? - React Material-UI Autocomplete: How to clear multiple values (mui chips) selected in a Autocomplete field on changing the value in another field? 以编程方式在 material-ui 自动完成文本字段中设置值 - Programmatically set value in material-ui Autocomplete TextField 如何获取 material-ui 的自动完成 Select 的值? - How can I get the value of the Autocomplete Select of material-ui? Material-ui<autocomplete /> getOptionLabel - 将空字符串作为值传递 - Material-ui <Autocomplete /> getOptionLabel - passing empty string as value 使用 material-ui 自动完成获取所选值的“ID” - Get the "ID" of the selected value with material-ui autocomplete 无法使用 select 项目从 material-ui 自动完成和 onchange 获取 event.target.value - Cant get event.target.value using select item from material-ui autocomplete with onchange 在 onSelect 处从 material-ui 自动完成检索条目的键,而不是值 - Retrieving the key for the entry from material-ui autocomplete at onSelect, instead of value material-ui DropDown,具有imag的选定值 - material-ui DropDown with imag on selected value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM