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

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

在 ReactJS 中,模態主體和頁腳在加載數據表單 API 時顯示兩次

在 ReactJS 中,模態主體和頁腳在加載數據表單 API 時顯示兩次

MMMHUHU 2022-08-18 10:12:18
我正在嘗試從 laravel 路由加載數據,并在模態中請求 axios 請求,但模態主體和 btns 在模態內顯示兩次。為什么會這樣?以下是我的代碼供參考。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>這是我在渲染中得到的圖像我已經嘗試加載完整的數據,現在我只通過添加項目名稱向您展示,但模態數據已經在project.map區域下顯示兩次,請指導我為什么會發生這種情況
查看完整描述

2 回答

?
qq_笑_17

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

因此,您正在獲取數據,將其存儲在本地存儲中,并且提取的數據以數組的形式存在。


此外,您正在映射此提取的數組,并且對于數組中的每個項目,您將返回一個 React 片段。


這個 React Fragment 正在渲染 Modal.Header、Modal.Body、Modal.Footer。


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


Modal.Header 和 Modal.Footer 應該在 map 函數之外。


<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>

更新:對組件DidMount調用進行以下更改。在你獲得結果后設置狀態之前,我添加了一些注釋。


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);

  });

 }

在此之后,您可以像使用任何其他對象一樣正常使用項目數據。我希望這能解決問題。如果沒有,請分享您當前使用的確切代碼。


查看完整回答
反對 回復 2022-08-18
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

你是一個數組,似乎你從后端獲取了兩個項目,所以當你這樣做時,你最終會創建兩個模態主體和頁腳。state.singleProjectsingleProject.map


相反,您應該更改為并讓您的API返回一個對象并替換您的模式代碼,如下所示: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>

希望它有幫助!


查看完整回答
反對 回復 2022-08-18
  • 2 回答
  • 0 關注
  • 88 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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