react 下拉框內(nèi)容回顯的實(shí)現(xiàn)思路
需要實(shí)現(xiàn)效果如下

目前效果如下

思路 : 將下拉框選項(xiàng)的value和label一起存儲(chǔ)到state中 , 初始化表單數(shù)據(jù)時(shí) , 將faqType對(duì)應(yīng)的label查找出來(lái)并設(shè)置到Form.Item中 , 最后修改useEffect
舊代碼
//可以拿到faqType為0 但是卻沒(méi)有回顯出下拉框的內(nèi)容 我需要faqType為0 回顯出下拉框內(nèi)容為對(duì)應(yīng)的label
<Form
form={form2}
initialValues={{
question: currentRecord.question || '',
faqType: currentRecord.faqType || '',
}}
>
<Form.Item
label='問(wèn)題類型'
name='faqType'
colon={false}
rules={[{ required: true, message: '請(qǐng)輸入問(wèn)題類型' }]}
>
<Select
onChange={value => {
setSelectedCon1(value)
form2.setFieldsValue({ faqType: value })
}}
allowClear
showSearch
placeholder='請(qǐng)輸入問(wèn)題類型'
style={{ width: 300, height: 40 }}
options={[
{ value: 0, label: '如何使用' },
{ value: 1, label: '常見(jiàn)情況' },
{ value: 2, label: '日常維護(hù)' },
{ value: 3, label: '如何更換' }
]}
/>
</Form.Item>
// 彈窗內(nèi)部數(shù)據(jù)回顯
useEffect(() => {
if (currentRecord) {
form2.setFieldsValue({
question: currentRecord.question || '',
faultInformationId: currentRecord.faultInformationId || '',
faqType: currentRecord.faqType || '',
answer: currentRecord.answer || ''
})
}
}, [currentRecord, form2])
解決問(wèn)題的代碼
const [faqTypes, setFaqTypes] = useState([
{ value: 0, label: '如何使用' },
{ value: 1, label: '常見(jiàn)情況' },
{ value: 2, label: '日常維護(hù)' },
{ value: 3, label: '如何更換' }
]);
<Form.Item
label='問(wèn)題類型'
name='faqType'
colon={false}
rules={[{ required: true, message: '請(qǐng)輸入問(wèn)題類型' }]}
>
<Select
onChange={value => {
setSelectedCon1(value)
form2.setFieldsValue({ faqType: value })
}}
allowClear
showSearch
placeholder='請(qǐng)輸入問(wèn)題類型'
style={{ width: 300, height: 40 }}
options={faqTypes.map(type => ({ value: type.value, label: type.label }))}
/>
</Form.Item>
useEffect(() => {
if (currentRecord) {
const selectedFaqType = faqTypes.find(type => type.value === currentRecord.faqType);
form2.setFieldsValue({
question: currentRecord.question || '',
faultInformationId: currentRecord.faultInformationId || '',
faqType: selectedFaqType ? selectedFaqType.label : '',
answer: currentRecord.answer || ''
})
}
}, [currentRecord, form2, faqTypes])到此這篇關(guān)于react 下拉框內(nèi)容回顯的文章就介紹到這了,更多相關(guān)react 下拉框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React新擴(kuò)展函數(shù)setState與lazyLoad及hook介紹
這篇文章主要介紹了React新擴(kuò)展函數(shù)setState與lazyLoad及hook,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12
詳解react-navigation6.x路由庫(kù)的基本使用
最近兩個(gè)項(xiàng)目都用到了React Navigation,所以就研究一下如何使用,本文主要介紹了react-navigation6.x路由庫(kù)的基本使用,感興趣的可以了解一下2021-11-11
React中useCallback 的基本使用和原理小結(jié)
React的useCallback Hook用于緩存函數(shù)定義,避免組件重渲染時(shí)重復(fù)創(chuàng)建函數(shù)實(shí)例,本文就來(lái)介紹一下React 中 useCallback 的基本使用和原理,感興趣的可以了解一下2025-11-11
React生命周期方法之componentDidMount的使用
這篇文章主要介紹了React生命周期方法之componentDidMount的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
react組件從搭建腳手架到在npm發(fā)布的步驟實(shí)現(xiàn)
這篇文章主要介紹了react組件從搭建腳手架到在npm發(fā)布的步驟實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
詳解React如何優(yōu)雅地根據(jù)prop更新state值
這篇文章主要為大家詳細(xì)介紹了React如何優(yōu)雅地實(shí)現(xiàn)根據(jù)prop更新state值,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解下2023-11-11

