[BUG] - Select Component defaultSelectedKeys props doesn't react to a state change in nextui
NextUI Version
2.2.9
Describe the bug
When the component first initial, I set useState like this:
const [category, setCategory] = useState('')
Then I import the NextUI Select component
<Select
items={productCategory}
name='product_category'
label='Danh mục sản phẩm'
selectionMode='single'
selectedKeys={categorySelectedValue}
onSelectionChange={setCategorySelectedValue}
defaultSelectedKeys={[{product.category}]}
>
{(category) => <SelectItem key={category.key}>{category.title}</SelectItem>}
</Select>
Despite that I have some logic for setCategory to a specific value from my database (fetch API), the NextUI Select still warns me that the '' (the initial value) is not presented in the collection. It seems like the defaultSelectedKeys still takes my initial value and I do not know why. It should be re-rendered to another specific value, did I miss something in my code?
Your Example Website or App
No response
Steps to Reproduce the Bug or Issue
- const [category, setCategory] = useState('')
- setCategory('VALUE FROM DATABASE')
- defaultSelectedKeys={[product.category]} // default value should be changed too
Expected behavior
I expect the result should be like this:
- DefaultSelectedKey is initial value based on useState()
- The useState() rerender then the defaultSelectedKey should be changed too
- Finally, Display the label of the new defaultSelectedKey in Select Component
Screenshots or Videos
No response
Operating System Version
Windows
Browser
Edge
ENG-631 [BUG] - Select Component defaultSelectedKeys props doesn't react to a state change in nextui
defaultSelectedKeys is uncontrolled and won't be updated by setCategory. You can just use selectedKeys which is a controlled. You may also need isLoading for your async loading (example).
I have some logic for setCategory to a specific value from my database (fetch API), the NextUI Select still warns me that the '' (the initial value) is not presented in the collection
I've made the fix in here
defaultSelectedKeysis uncontrolled and won't be updated bysetCategory. You can just useselectedKeyswhich is a controlled. You may also needisLoadingfor your async loading (example).I have some logic for setCategory to a specific value from my database (fetch API), the NextUI Select still warns me that the '' (the initial value) is not presented in the collection
I've made the fix in here
Yeah, you're right I have done it using selectedKey. Thank you so much, bro.