ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 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
Designed by Tistory.