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渲染機(jī)制及相關(guān)優(yōu)化方案
這篇文章主要介紹了react中的渲染機(jī)制以及相關(guān)的優(yōu)化方案,內(nèi)容包括react渲染步驟、concurrent機(jī)制以及產(chǎn)生作用的機(jī)會(huì),簡單模擬實(shí)現(xiàn) concurrent mode,基于作業(yè)調(diào)度優(yōu)先級(jí)的思路進(jìn)行項(xiàng)目優(yōu)化的兩個(gè)hooks,感興趣的小伙伴跟著小編一起來看看吧2023-07-07
react.js 獲取真實(shí)的DOM節(jié)點(diǎn)實(shí)例(必看)
下面小編就為大家?guī)硪黄猺eact.js 獲取真實(shí)的DOM節(jié)點(diǎn)實(shí)例(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
React-Hooks之useImperativeHandler使用介紹
這篇文章主要為大家介紹了React-Hooks之useImperativeHandler使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
react開發(fā)中如何使用require.ensure加載es6風(fēng)格的組件
本篇文章主要介紹了react開發(fā)中如何使用require.ensure加載es6風(fēng)格的組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
React styled-components設(shè)置組件屬性的方法
這篇文章主要介紹了styled-components設(shè)置組件屬性的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
基于React實(shí)現(xiàn)虛擬滾動(dòng)的方案詳解
這篇文章將以固定高度和非固定高度兩種場景展開React中虛擬滾動(dòng)的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03

