react中使用Modal.confirm數(shù)據(jù)不更新的問題完美解決方案
在使用Modal.confirm的時(shí)候今天發(fā)現(xiàn)了個(gè)疑惑的問題,為什么我明明從新set了數(shù)據(jù)而頁面視圖沒有變化,查了一下官方文檔找到了答案,解決了這個(gè)問題,特意在這里留下痕跡。
import { Button, Col, Form, Input, Modal, Radio, Row, Select, Space, Spin } from 'antd' let modal = null export default function (props) { const [typeStr, settypeStr] = useState('') const [lookIPorAS, setlookIPorAS] = useState('ip') const returnModalFun = () => { return <Row className="m-b-10-px"> <Col span={16} key={lookIPorAS}> <Radio.Group onChange={onchangeFun} value={lookIPorAS}> <Radio value="ip">IP</Radio> <Radio value="as">AS</Radio> </Radio.Group> </Col> </Row> } const onchangeFun = (e) => { setlookIPorAS(e.target.value) } const detailsFun = (item) => { setItemObj(item) const { type } = item if (type === '濫用') { // settypeStr('abuseString') modal = Modal.confirm({ title: '請(qǐng)選擇要查看的類型', content: returnModalFun(), onOk: () => { }, }) } else if (type === 'aaa') { settypeStr('type1') setVisible(true) } else if (type === 'bbb') { settypeStr('type2') setVisible(true) } } return ( null ) }
上述的代碼是有問題的,據(jù)說會(huì)發(fā)生我上述的問題,數(shù)據(jù)并不能更新。首先看一下官方的解釋:
我們只需要在生成Modal的時(shí)候接收返回的實(shí)例對(duì)象然后調(diào)用update方法即可更新數(shù)據(jù)
useEffect(() => { modal && modal.update({ title: '請(qǐng)選擇要查看的類型', content:returnModalFun() , }); }, [lookIPorAS])
全部代碼:
```javascript import { Button, Col, Form, Input, Modal, Radio, Row, Select, Space, Spin } from 'antd' let modal = null export default function (props) { const [typeStr, settypeStr] = useState('') const [lookIPorAS, setlookIPorAS] = useState('ip') useEffect(() => { modal && modal.update({ title: '請(qǐng)選擇要查看的類型', content:returnModalFun() , }); }, [lookIPorAS]) const returnModalFun = () => { return <Row className="m-b-10-px"> <Col span={16} key={lookIPorAS}> <Radio.Group onChange={onchangeFun} value={lookIPorAS}> <Radio value="ip">IP</Radio> <Radio value="as">AS</Radio> </Radio.Group> </Col> </Row> } const onchangeFun = (e) => { setlookIPorAS(e.target.value) } const detailsFun = (item) => { const { type } = item if (type === '濫用') { // settypeStr('abuseString') modal = Modal.confirm({ title: '請(qǐng)選擇要查看的類型', content: returnModalFun(), onOk: () => { }, }) } else if (type === 'aaa') { settypeStr('type1') setVisible(true) } else if (type === 'bbb') { settypeStr('type2') setVisible(true) } } return ( null ) }
到此這篇關(guān)于react中使用Modal.confirm數(shù)據(jù)不更新的問題解決的文章就介紹到這了,更多相關(guān)react 用Modal.confirm數(shù)據(jù)不更新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React useMemo與useCallabck有什么區(qū)別
useCallback和useMemo是一樣的東西,只是入?yún)⒂兴煌瑄seCallback緩存的是回調(diào)函數(shù),如果依賴項(xiàng)沒有更新,就會(huì)使用緩存的回調(diào)函數(shù);useMemo緩存的是回調(diào)函數(shù)的return,如果依賴項(xiàng)沒有更新,就會(huì)使用緩存的return2022-12-12React?Hook?Form?優(yōu)雅處理表單使用指南
這篇文章主要為大家介紹了React?Hook?Form?優(yōu)雅處理表單使用指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03react頁面中存在多個(gè)input時(shí)巧妙設(shè)置value屬性方式
這篇文章主要介紹了react頁面中存在多個(gè)input時(shí)巧妙設(shè)置value屬性方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05React組件、狀態(tài)管理、代碼優(yōu)化的技巧
文章總結(jié)了React組件設(shè)計(jì)、狀態(tài)管理、代碼組織和優(yōu)化的技巧,它涵蓋了使用Fragment、props解構(gòu)、defaultProps、key和ref的使用、渲染性能優(yōu)化等方面2024-11-11React18中請(qǐng)求數(shù)據(jù)的官方姿勢(shì)適用其他框架
這篇文章主要為大家介紹了官方回答在React18中請(qǐng)求數(shù)據(jù)的正確姿勢(shì)詳解,同樣也適用其他框架,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07React 進(jìn)入頁面后自動(dòng) focus 到某個(gè)輸入框的解決方案
React.js 當(dāng)中提供了 ref 屬性來幫助我們獲取已經(jīng)掛載的元素的 DOM 節(jié)點(diǎn),你可以給某個(gè) JSX 元素加上 ref屬性,這篇文章主要介紹了React 進(jìn)入頁面以后自動(dòng) focus 到某個(gè)輸入框,需要的朋友可以參考下2024-02-02react實(shí)現(xiàn)pure render時(shí)bind(this)隱患需注意!
這篇文章主要為大家詳細(xì)介紹了值得你在react實(shí)現(xiàn)pure render的時(shí)候,需要注意的bind(this)隱患,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03解決React報(bào)錯(cuò)You provided a `checked` prop&n
這篇文章主要為大家介紹了React報(bào)錯(cuò)You provided a `checked` prop to a form field的解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12