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

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

Reactjs 需要結(jié)合 Render 中的 const 和 componentDidMount

Reactjs 需要結(jié)合 Render 中的 const 和 componentDidMount

問(wèn)題:我能夠在 componentDidMount 中使用 axios 進(jìn)行 web api 調(diào)用,然后在 Render 中我可以使用 .map 循環(huán)遍歷數(shù)據(jù)并使用 table 和 bootstrap 類創(chuàng)建 html。我遇到的問(wèn)題是我需要有按鈕來(lái)顯示它們是否是本地存儲(chǔ)中的成員。因此,現(xiàn)在我認(rèn)為我需要將所有內(nèi)容移出 Render() 并放置循環(huán)和 if/else 檢查以在 webapi .then 部分中顯示正確的按鈕使用 axios 調(diào)用中的 html 表和數(shù)據(jù)執(zhí)行此操作的最佳方法是什么?componentDidMount 與 Axios web api 調(diào)用: componentDidMount() {    webApi.get('sai/getofflinemembers?userId=N634806')        .then((event) => {          // Instead of looping with .map in the render(), thinking I need to          // do the looping inside here to correctly create the correct element           // with the right color button and text           for (var i = 0; i < event.data.length; i++) {             //check with local storage if the memberid exist             // need to use a different button style and text             if (localStorage.getItem(event.data[i]["Member_ID"]) != null) {                //document.getElementById(event.data[i]["Member_ID"]).classList.remove('btn-warning');                //above would be after                 // need to DO ALL in render  const contents html table in here                 // <button id={item.Member_ID} type="button" onClick={(e) => this.downloadUser(item.Member_ID,e)}                   className="btn btn-success">SAI</button>             }          }          this.setState({                data: event.data          });    });使成為: render() {    const contents = this.state.data.map(item => (         <tr>              <td>{item.Member_Name}</td>               <td>{item.Member_ID}</td>              <td>{item.Member_DOB}</td>              <td>{item.ProgramType}</td>              <td>                <button id={item.Member_ID} type="button" onClick={(e) => this.downloadUser(item.Member_ID,e)}                   className="btn btn-warning">Ready for Download</button>              </td>         </tr>        ))        return(         ....         {contents}         ....        )    }
查看完整描述

2 回答

?
素胚勾勒不出你

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

生命周期方法喜歡componentDidMount并render具有標(biāo)準(zhǔn)的角色隔離。通常,前者旨在執(zhí)行一些 API 調(diào)用(如您所擁有的),而后者,render 用于生成標(biāo)記。componentDidMount無(wú)論如何,您應(yīng)該避免嘗試生成標(biāo)記,而不是像它真的可能那樣。


聽(tīng)起來(lái)您要做的就是檢查從 API 返回的項(xiàng)目是否在 localStorage 中可用,然后className根據(jù)其存在有條件地更改該按鈕的 。您可以創(chuàng)建一個(gè)輔助函數(shù)來(lái)幫助您實(shí)現(xiàn)這一目標(biāo)。


checkItem = (item) => {

    let className;

    if(localStorage.getItem(item["Member_ID"]) != null){

       className = "btn btn-success"

    } else {

       className = "btn btn-warning"

    }

    return className

}

然后,由于您正在映射項(xiàng)目數(shù)組,因此我們可以根據(jù)您的標(biāo)記調(diào)用此函數(shù)。


 render() {


    const contents = this.state.data.map(item => (

         <tr>

              <td>{item.Member_Name}</td> 

              <td>{item.Member_ID}</td>

              <td>{item.Member_DOB}</td>

              <td>{item.ProgramType}</td>

              <td>

                <button id={item.Member_ID} type="button" onClick={(e) => this.downloadUser(item.Member_ID,e)} 

                  className={this.checkItem(item)}>Ready for Download</button>

              </td>

         </tr>

        ))

        return(

         ....

         {contents}

         ....

        )

    }


查看完整回答
反對(duì) 回復(fù) 2021-11-04
?
侃侃爾雅

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

嘗試這個(gè):


componentDidMount{

    webApi.get('sai/getofflinemembers?userId=N634806')

    .then(event => {

         this.setState({

              data: event.data

         });

    })

}


const isExistLocalStorage = value => {

    return localStorage.getItem(value) ? true : false;

}

render() {

    const { data } = this.state;

    return (

        data && data.map((item, index) => {

            const isExist = isExistLocalStorage(item.Member_ID);

            <tr key={index}>

                <td>{item.Member_Name}</td>

                <td>{item.Member_ID}</td>

                <td>{item.Member_DOB}</td>

                <td>{item.ProgramType}</td>

                <td>

                    <button

                        id={item.Member_ID}

                        type="button"

                        onClick={(e) => this.downloadUser(item.Member_ID, e)}

                        className={`btn ${isExist ? "btn-success" : "btn-warning"}`}

                    > {isExist ? "Ready for Download" : "Not ready"}</button>

                </td>

            </tr>

        })

    )

}


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

添加回答

舉報(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)