亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我怎樣才能重構我的代碼以確保沒有重復

我怎樣才能重構我的代碼以確保沒有重復

jeck貓 2023-11-12 15:33:29
我將在我正在處理的項目中調用三個不同的端點。除了我將調用的不同 url 之外,處理請求的函數都是相同的。下面是我的代碼示例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();    }我該如何編寫此代碼以避免重復,因為除了基本網址之外,所有內容都是相同的。
查看完整描述

3 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

為您的帖子請求創建一個函數:


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)

    })

}

然后你可以在你的代碼中使用這個函數,如下所示:


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)

}


查看完整回答
反對 回復 2023-11-12
?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

1


請嘗試這個。


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)

        })

}


查看完整回答
反對 回復 2023-11-12
?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

其他答案中表達的想法的稍微簡潔的版本可能如下所示:


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}

)

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


不過,無論是在本文中還是在原版中,我對全球訪問user. 這也可以作為參數傳遞嗎?


查看完整回答
反對 回復 2023-11-12
  • 3 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號