-
React.js(7) - SweetAlert2์น ๊ฐ๋ฐ/React.js 2023. 4. 5. 17:17
๐ฉ๐ป SweetAlert2 ์ฌ์ฉํ๊ธฐ
๐ SweetAlert2๋?
- ๋์์ธ๊ณผ ๊ธฐ๋ฅ์ ์๋์ฐฝ์ ์ ๊ณตํด์ค๋ค.
- ์ฃผ๋ก ์ฌ์ฉ์์๊ฒ ํ์ํ ์ ๋ณด๋ฅผ ์๋ฆผ์ฐฝ์ผ๋ก ์ถ๋ ฅํด์ฃผ๋๋ก ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
๐ ์ค์น
npm install sweetalert2
โ Sweetalert2.js
import React from "react"; import { Component } from "react"; import Swal from "sweetalert2"; class R056_Sweetalert2Basic extends Component { componentDidMount() { //Swal.fire('1.SweetAlert') //alert('2. alert()') Swal.fire('1. SweetAlert').then(result => {alert('2. result.value :'+result.value)}) } render() { return ( <h1>sweetalert2</h1> ) } } export default R056_Sweetalert2Basic
๐ ํน์ง
- Swal์ ๋น๋๊ธฐ์ ์ธ ์ฝ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
Swal.fire('1.SweetAlert') alert('2. alert()')
์ด๋ ๊ฒ ์ฐ๊ฒ๋๋ฉด... sweetlalert2๋ณด๋ค alert๊ฐ ๋จผ์ ๋์ํ๊ฒ ๋๋ค. ์ด์ ๋ sweetalert2๋ ๋น๋๊ธฐ์ ์ผ๋ก ๋์ํ์ฌ Swal.fire() ํจ์๋ฅผ ์คํ์์ผ ๋๊ณ ์๋ฃ ์ฌ๋ถ์ ์๊ด์์ด ๊ทธ๋ฅ alert๋ฅผ ๋์์ํค๊ฒ ๋๋ค.
๐ vs alert() : ๋๊ธฐ์ ์ฝ๋
- ํ์ธ๋ฒํผ์ ๋๋ฅด์ง ์์ผ๋ฉด ๋ค์ ๋์์ผ๋ก ๋์ด๊ฐ์ง ์๊ธฐ ๋๋ฌธ์ alert ํ์ธ์ ๋๋ฅด์ง ์๊ฒ๋๋ฉด ์์์ด Swal.fire์ ๋์์ํค์ง ์๊ฒ ๋จ
Swal.fire('1. SweetAlert').then(result => {alert('2. result.value :'+result.value)})
ํ๋ก๋ฏธ์ค์ then ํจ์๋ฅผ ์ฌ์ฉํ์ฌ Swal.fire ํจ์์ ๊ฒฐ๊ณผ๊ฐ(result)๊ฐ ๋ฐํ๋๋ค๋ ์กฐ๊ฑด ํ์ alert ํจ์๋ฅผ ์คํ์ํจ๋ค.
- Swal.fire() ํจ์ ์๋ฆผ์ฐฝ์์ OK ๋ฒํผ์ ๋๋ฅด๊ฒ ๋๋ฉด result ๊ฐ์ด true๋ก ๋ฐํ๋จ
๐ ํ๋ก๋ฏธ์ค๋?
- ๋น๋๊ธฐ์ ํจ์๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ก ํ๋ ๊ฒ
- then์ ์ด์ ํจ์๊ฐ ์์ ํ ์๋ฃ๊ฐ ๋์ด์ผ ์คํ์ด ๋๋ค.
๐ Position
โ Sweetalert2Position.js
import React, {Component} from 'react'; import Swal from 'sweetalert2'; class R057_Sweetalert2Position extends Component { saveAlert = (flag, positionflag, e) =>{ Swal.fire({ position:positionflag, icon:'success', title:flag+'๋์์ต๋๋ค.', showConfirmButton : false, timer:1500 }) } render() { return( <> <h1>sweetalert2</h1> <button onClick={e=>this.saveAlert('์ ์ฅ','center')}>์ ์ฅ</button> <button onClick={e=>this.saveAlert('์์ ', 'bottom-end')}>์์ </button> </> ) } }
๐ saveAlert -Position ๋ฉ์๋
saveAlert = (flag, positionflag, e) =>{ Swal.fire({ position:positionflag, icon:'success', title:flag+'๋์์ต๋๋ค.', showConfirmButton : false, timer:1500 }) }
๐ค SaveAlert- Position ๋ฉ์๋ ํ๋ผ๋ฏธํฐ ์ค๋ช
๐position : positionflag(์๋ฆผ์ฐฝ ์์น ์ค์ )
ใด top : ์ค์์๋จ
ใด top-start : ์ข์ธก ์๋จ
ใด top-end : ์ฐ์ธก ์๋จ
ใด center : ์ ์ค์
ใด center-start : ์ค์์ข์ธก
ใด center-end : ์ค์ ์ฐ์ธก
ใด bottom : ์ค์ํ๋จ
ใด bottom-start : ์ข์ธก ํ๋จ
ใด bottom-end : ์ฐ์ธก ํ๋จ
ใด title : ์ถ๋ ฅ ๋ฉ์ธ์ง
ใด showConfirmButton : ํ์ธ ๋ฒํผ(x ๋ฒํผ) ์ถ๋ ฅ ์ฌ๋ถ
ใด timer : ๋ช์ด ๋์ ์ด alert๋ฅผ ๋์ธ ๊ฒ์ธ์ง
๐ Confirm
- ์ฌ์ฉ์์ ์ค์๋ก ๋ฐ์ดํฐ๊ฐ ์ญ์ ๋๋ฆญ ์ ์ ์ค์ ์ญ์ ์์ ์ ์คํํ๊ธฐ ์ ์ ๋ค์ ํ ๋ฒ ํ์ธํ๋ ์๋ฆผ์ฐฝ
import React, {Component} from 'react'; import Swal from 'sweetalert2'; class R058_Sweetalert2Confirm extends Component { deletAlert=()=>{ Swal.fire({ title:'์ ๋ง ์ญ์ ํ์๊ฒ ์ต๋๊น?', icon:'question', showCancelButton : true, confirmButtonColor:'#48088A', confirmButtonText:'์', cancelButtonText:'์๋์ค', }).then((result) => { if (result.value){ document.getElementById('deletId').remove(); Swal.fire( 'Deleted', 'sweetalert2 ์ญ์ ์๋ฃ', 'success' ) } }) } render() { return( <> <h2>sweetalert2</h2> <button onClick={e=>this.deletAlert()}>์ญ์ </button> </> ) } } export default R058_Sweetalert2Confirm;
๐ SweetAlert-Confirm ๋ฉ์๋
deletAlert=()=>{ Swal.fire({ title:'์ ๋ง ์ญ์ ํ์๊ฒ ์ต๋๊น?', icon:'question', showCancelButton : true, confirmButtonColor:'#48088A', confirmButtonText:'์', cancelButtonText:'์๋์ค', }).then((result) => { if (result.value){ document.getElementById('deletId').remove(); Swal.fire( 'Deleted', 'sweetalert2 ์ญ์ ์๋ฃ', 'success' ) } }) }
'์น ๊ฐ๋ฐ > React.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React.js(9) - axios (0) 2023.05.03 React.js(8) - FetchGET&POST (0) 2023.04.05 React.js(6) - Reactstrap ์์ ์ค๋ช (2) (0) 2023.04.05 React.js(5) - Reactstrap ์์ ์ค๋ช (1) (0) 2023.03.29 React.js(4) - Proptypes (0) 2023.03.29