第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我怎樣才能重構(gòu)我的代碼以確保沒(méi)有重復(fù)

我怎樣才能重構(gòu)我的代碼以確保沒(méi)有重復(fù)

jeck貓 2023-11-12 15:33:29
我將在我正在處理的項(xiàng)目中調(diào)用三個(gè)不同的端點(diǎn)。除了我將調(diào)用的不同 url 之外,處理請(qǐng)求的函數(shù)都是相同的。下面是我的代碼示例const handleSubmitPhoneNumber = (e, next) => {        e.preventDefault();        const payload = {            "phone": user.phone        }        const postPhoneNumber = async () => {            setLoading(true)            await axios.post("https://jsonplaceholder.typicode.com/users", payload)                .then(response => {                    setLoading(false)                    console.log(response)                    let res = response;                    if (res.data.id === 11) {                        next();                    }                })                .catch(error => {                    console.log(error)                });        }        postPhoneNumber();    }    const handleSubmitVerificationCode = (e, next) => {        e.preventDefault();        const payload = {            "verificationCode": user.verificationCode        }        const postVerificationCode = async () => {            setLoading(true)            await axios.post("https://jsonplaceholder.typicode.com/users", payload)                .then(response => {                    setLoading(false)                    console.log(response)                    let res = response;                    if (res.data.id === 11) {                        next();                    }                })                .catch(error => {                    console.log(error)                })        }        postVerificationCode();    }我該如何編寫(xiě)此代碼以避免重復(fù),因?yàn)槌嘶揪W(wǎng)址之外,所有內(nèi)容都是相同的。
查看完整描述

3 回答

?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊

為您的帖子請(qǐng)求創(chuàng)建一個(gè)函數(shù):


async function POST(url, payload, next) {

  setLoading(true)

  await axios.post(url, payload)

    .then(response => {

      setLoading(false)

      console.log(response)

      let res = response;

      if (res.data.id === 11) {

        next();

      }

    })

    .catch(error => {

      console.log(error)

    })

}

然后你可以在你的代碼中使用這個(gè)函數(shù),如下所示:


const handleSubmitPhoneNumber = (e, next) => {

    e.preventDefault();

    const payload = {

        "phone": user.phone

    }

    POST("https://jsonplaceholder.typicode.com/users", payload, next)


const handleSubmitVerificationCode = (e, next) => {

    e.preventDefault();

    const payload = {

        "verificationCode": user.verificationCode

    }

    POST("https://jsonplaceholder.typicode.com/users", payload, next)

}


查看完整回答
反對(duì) 回復(fù) 2023-11-12
?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

1


請(qǐng)嘗試這個(gè)。


const handleSubmitPhoneNumber = (e, next) => {

    e.preventDefault();

    const payload = {

        "phone": user.phone

    }

    postMethod("https://jsonplaceholder.typicode.com/users", payload, next);

}


const handleSubmitVerificationCode = (e, next) => {

    e.preventDefault();

    const payload = {

        "verificationCode": user.verificationCode

    }

    postMethod("https://jsonplaceholder.typicode.com/users", payload, next);

}


const postMethod = async (url, payload, next) => {

    setLoading(true)

    await axios.post(url, payload)

        .then(response => {

            setLoading(false)

            console.log(response)

            let res = response;

            if (res.data.id === 11) {

                next();

            }

        })

        .catch(error => {

            console.log(error)

        })

}


查看完整回答
反對(duì) 回復(fù) 2023-11-12
?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

其他答案中表達(dá)的想法的稍微簡(jiǎn)潔的版本可能如下所示:


const postMethod = (url, getPayload) => async (e, next) => {

    e.preventDefault();

    setLoading(true)

    await axios.post(url, getPayload())

        .then(response => {

            setLoading(false)

            console.log(response)

            let res = response;

            if (res.data.id === 11) {

                next();

            }

        })

        .catch(error => {

            console.log(error)

        })

}


const handleSubmitPhoneNumber = postMethod (

  "https://jsonplaceholder.typicode.com/users",

  () => {phone: user.phone}

)


const handleSubmitVerificationCode = postMethod (

  "https://jsonplaceholder.typicode.com/users",

  () => {verificationCode: user.verificationCode}

)

我更喜歡這個(gè)的主要原因是這些處理程序的處理方式e在next這些處理程序之間沒(méi)有變化,因此理想情況下它屬于公共代碼。


不過(guò),無(wú)論是在本文中還是在原版中,我對(duì)全球訪問(wèn)user. 這也可以作為參數(shù)傳遞嗎?


查看完整回答
反對(duì) 回復(fù) 2023-11-12
  • 3 回答
  • 0 關(guān)注
  • 254 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)