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

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

在 ReactJS 中,模態(tài)主體和頁(yè)腳在加載數(shù)據(jù)表單 API 時(shí)顯示兩次

在 ReactJS 中,模態(tài)主體和頁(yè)腳在加載數(shù)據(jù)表單 API 時(shí)顯示兩次

MMMHUHU 2022-08-18 10:12:18
我正在嘗試從 laravel 路由加載數(shù)據(jù),并在模態(tài)中請(qǐng)求 axios 請(qǐng)求,但模態(tài)主體和 btns 在模態(tài)內(nèi)顯示兩次。為什么會(huì)這樣?以下是我的代碼供參考。class PopUp extends React.Component{ constructor(props) {    super(props);    this.state={        singleProject:[],        errs:''    }}singleData(){    const token = document.querySelector('meta[name="csrf-token"]');    const fd={        '_token':token.content,        'id':this.props.id,    }    return fd;}componentDidMount() {    //reuest for services    Axios.post('/single/project',this.singleData()).then(resp =>{        this.setState({singleProject:resp.data});        console.log(resp.data)    }).catch(err=>{        this.setState({errs:err})        // console.log(err)    })}render() {    const project= this.state.singleProject;    return(        <div>            <Modal show={this.props.show} onHide={this.props.onHide} size="xl" aria-labelledby="contained-modal-title-vcenter" centered>                {                    project.map(data=>(                      <React.Fragment key={data.id}>                                <Modal.Header closeButton>                                    <Modal.Title id="contained-modal-title-vcenter">                                        {data.project_name}                                    </Modal.Title>                                </Modal.Header>                                <Modal.Body  >                                <h4>Centered Modal</h4>                            <p>                            Cras mattis consectetur purus sit amet fermentum. Cras justo odio,                            dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac                            consectetur ac, vestibulum at eros.                            </p>這是我在渲染中得到的圖像我已經(jīng)嘗試加載完整的數(shù)據(jù),現(xiàn)在我只通過(guò)添加項(xiàng)目名稱向您展示,但模態(tài)數(shù)據(jù)已經(jīng)在project.map區(qū)域下顯示兩次,請(qǐng)指導(dǎo)我為什么會(huì)發(fā)生這種情況
查看完整描述

2 回答

?
qq_笑_17

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

因此,您正在獲取數(shù)據(jù),將其存儲(chǔ)在本地存儲(chǔ)中,并且提取的數(shù)據(jù)以數(shù)組的形式存在。


此外,您正在映射此提取的數(shù)組,并且對(duì)于數(shù)組中的每個(gè)項(xiàng)目,您將返回一個(gè) React 片段。


這個(gè) React Fragment 正在渲染 Modal.Header、Modal.Body、Modal.Footer。


您應(yīng)該考慮使用不同的方法,而不是此方法。基本上,您需要將所有項(xiàng)目都顯示在模式中。因此,將每個(gè)項(xiàng)目視為


Modal.Header 和 Modal.Footer 應(yīng)該在 map 函數(shù)之外。


<Modal>

 <Modal.Header>

  Modal Heading

 </Modal.Header>

   {data.map(project => (

      <div class='project'>

       <div class='name'>{project.name}</div>

       <div class='description'>{project.description}</div>

       <div class='project_link'>

         <div class='live_link'>{data.live_link}</div>

         <div class='github_link'>{data.github_link}</div>

       </div>

      </div>

    )})}

 <Modal.Footer>

  Footer Info

 </Modal.Footer>

</Modal>

更新:對(duì)組件DidMount調(diào)用進(jìn)行以下更改。在你獲得結(jié)果后設(shè)置狀態(tài)之前,我添加了一些注釋。


componentDidMount() {

//reuest for services

Axios.post("/single/project", this.singleData())

  .then(resp => {

    //here the data you are getting is in the form of an array.

    //but you don't need that entire array.

    //all you need is the object sitting at index 1

    //so instead of setting {resp.data} set {resp.data[1]}

    this.setState({

       singleProject: (resp.data && resp.data[1]) || {} 

    });

    console.log(resp.data);

   })

  .catch(err => {

    this.setState({ errs: err });

    console.log(err);

  });

 }

在此之后,您可以像使用任何其他對(duì)象一樣正常使用項(xiàng)目數(shù)據(jù)。我希望這能解決問(wèn)題。如果沒(méi)有,請(qǐng)分享您當(dāng)前使用的確切代碼。


查看完整回答
反對(duì) 回復(fù) 2022-08-18
?
搖曳的薔薇

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

你是一個(gè)數(shù)組,似乎你從后端獲取了兩個(gè)項(xiàng)目,所以當(dāng)你這樣做時(shí),你最終會(huì)創(chuàng)建兩個(gè)模態(tài)主體和頁(yè)腳。state.singleProjectsingleProject.map


相反,您應(yīng)該更改為并讓您的API返回一個(gè)對(duì)象并替換您的模式代碼,如下所示:state.singleProject{}


    <Modal show={this.props.show} onHide={this.props.onHide} size="xl" aria-labelledby="contained-modal-title-vcenter" centered>

          <React.Fragment key={project.id}>

            <Modal.Header closeButton>

              <Modal.Title id="contained-modal-title-vcenter">

                {project.project_name}

              </Modal.Title>

            </Modal.Header>

            <Modal.Body  >

              <h4>Centered Modal</h4>

              <p>

                Cras mattis consectetur purus sit amet fermentum. Cras justo odio,

                dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac

                consectetur ac, vestibulum at eros.

                            </p>

            </Modal.Body>

            <Modal.Footer>

              <a href={project.project_live_link} className="btn btn-primary">live</a>

              <a href={project.project_github_link} className="btn btn primary">github</a>

            </Modal.Footer>

          </React.Fragment>


        </Modal>

希望它有幫助!


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

添加回答

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