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

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

我正在嘗試在 React 中制作折疊/手風琴。但問題是,它不會觸發我想要切換的特定內容

我正在嘗試在 React 中制作折疊/手風琴。但問題是,它不會觸發我想要切換的特定內容

慕桂英546537 2022-10-21 09:40:32
我正在嘗試在 React 中制作折疊/手風琴。但問題是,它不會觸發我想要切換的特定內容,即當我點擊標題時,所有段落標簽都達到了最大高度。我知道這段代碼沒有錯誤,但我也不知道如何觸發特定的段落標簽。這是我的反應代碼:import React, {useState} from 'react'import style from './stylecss.module.css'import AnimateHeight from 'react-animate-height'import cx from 'classname'import globalStyles from '../Assets/global-styles/bootstrap.module.css'function Collapse() {  const item = [    {      heading : "HEADING 1",      para : "DATA 1"    },    {      heading : "HEADING 2",      para : "DATA 2"    }  ]    const [state, setState] = useState("0")    function displayPara (){    console.log(itemPlace)    if(state==0)      setState("auto")    else      setState(0)  }  const itemPlace = item.map(({heading, para}) => {    return (    <div>      <h5 onClick={ e => displayPara()}>        {heading}      </h5>      <AnimateHeight duration={ 500 } height={ `${state}` }>        <p>{para}</p>      </AnimateHeight>    </div>    )  })  return (    <div className={style.AboutPage}>      {itemPlace}    </div>  )}export default Collapse這是 CSS 代碼.AboutPage {    margin-top : 40px;    padding : 30px 6% 10px 6%;    background-color: rgb(250, 250, 250);    display: flex;    flex-direction: column;    text-align: left;}.paraDiv {    color : red !important;    width: 100%;    height : 100%;    background-color: chartreuse;    transition: max-height 0.5s;}.paraDiv.is-active {    height: 300px;}我不希望使用內置或預定義的折疊組件。
查看完整描述

2 回答

?
蕪湖不蕪

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

首先祝你好運!

您的代碼所說的是:當用戶單擊其中一項(特別是 h5 標簽)時,將高度設置為自動。你給每個項目這個高度,不區分它是否應該打開。

因此,您要做的是將您在“狀態”屬性中更改的高度僅應用于用戶單擊的項目。

我添加了幾行代碼來實現它。

  1. 添加另一個 state 屬性,稱為activeItem指示哪些是活動項(用戶單擊的項)。

  2. 在 displayPara 函數中添加一個參數:我們要顯示的活動索引。

  3. 僅將我們在函數中設置的高度應用于activeItem.

在這里你可以看到它。

import React, {useState} from 'react'

import style from './stylecss.module.css'

import AnimateHeight from 'react-animate-height'

import cx from 'classname'

import globalStyles from '../Assets/globalstyles/bootstrap.module.css'


function Collapse() {


  const item = [

    {

      heading : "HEADING 1",

      para : "DATA 1"

    },

    {

      heading : "HEADING 2",

      para : "DATA 2"

    }

  ]

  

  const [state, setState] = useState("0")

  const [activeItem, setActiveItem] = useState(null)

  

  function displayPara (index){

    console.log(itemPlace)

    setActiveItem(index)

    if(state==0)

      setState("auto")

    else

      setState(0)

  }


  const itemPlace = item.map(({heading, para}, index) => {

    return (

    <div>

      <h5 onClick={ e => displayPara(index)}>

        {heading}

      </h5>

      <AnimateHeight duration={ 500 } height={ `${activeItem === index ? state : 0}` }>

        <p>{para}</p>

      </AnimateHeight>

    </div>

    )

  })

  return (

    <div className={style.AboutPage}>

      {itemPlace}

    </div>

  )

}


export default Collapse

正如上面的評論之一所說,您也可以在不保持任何狀態的情況下使用 details 和 summary HTML 標簽在本地實現它。您還可以使用 CSS 將其樣式設置為與您的設計完全相同。


<details>

  <summary>Heading 1</summary>

  <p>Heading 1 Content</p>

</details>


查看完整回答
反對 回復 2022-10-21
?
皈依舞

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

你可以在本地做。


<details>

  <summary>Epcot Center</summary>

  <p>Epcot is a theme park at Walt Disney World Resort featuring exciting attractions, international pavilions, award-winning fireworks and seasonal special events.</p>

</details>


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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