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

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

單擊給定組件外部時,如何從 React.JS 組件內(nèi)的元素中刪除類?

單擊給定組件外部時,如何從 React.JS 組件內(nèi)的元素中刪除類?

慕斯王 2022-12-22 12:41:14
我正在嘗試模擬一種類似于在模式彈出窗口打開時單擊疊加層的行為。在 sidenav 組件外部單擊時,我想關(guān)閉當(dāng)前處于某個flyout模式的所有元素。我有一個多層嵌套導(dǎo)航菜單,存儲在它自己的組件中,Sidebar. 我有以下代碼處理clicks發(fā)生在Sidebar組件外部的代碼:class Sidebar extends React.Component {    ...    handleClick = (e) => {      if (this.node.contains(e.target)) {        return;      }        console.log('outside');    };      componentDidMount() {      window.addEventListener('mousedown', this.handleClick, false);    }    componentWillUnmount() {      window.removeEventListener('mousedown', this.handleClick, false);    }    render() {      return (          <div              ref={node => this.node = node}              className="sidebar"              data-color={this.props.bgColor}              data-active-color={this.props.activeColor}          >          {renderSideBar()}          </div>      );    }    ...}這部分工作正常 - 但是當(dāng)彈出菜單在單擊父菜單選項(xiàng)時顯示時,我希望它關(guān)閉當(dāng)前打開的任何彈出菜單。-| | - Menu Item 1  |  |-option 1 (currently open)  |-option 2 - Menu Item 2  |   |-option 1 (closed)  |-option 2 (closed, clicked to expand - this is when it should close [Menu Item 1/Option 1]<li>在映射包含菜單結(jié)構(gòu)的數(shù)據(jù)對象時,使用標(biāo)簽生成菜單項(xiàng)。有沒有一種方法可以基本上選擇所有具有 'collapse' / aria-expanded="true" 類的注冊對象并將其刪除?類似于如何jQuery選擇 dom 元素并操作它們。我知道這不是 React 工作的前提,它只是我想要模擬的行為的一個例子。
查看完整描述

1 回答

?
隔江千里

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個贊

據(jù)我了解,您想從另一個組件修改 DOM 子樹。為了實(shí)現(xiàn)您的目標(biāo),您可以使用ref.

ref當(dāng)您想直接訪問HtmlElement API時,使用會很有幫助- 在我的示例中,我使用animate(). 請閱讀文檔,因?yàn)樗枋隽烁?code>ref用例。

下面是當(dāng)用戶點(diǎn)擊時動畫 <Sidebar/>收縮的簡單例子<Content />。

const { useRef } = React;


function Main() {

  const sidebar = useRef(null);


  const handleClick = () => {

    sidebar.current.hide();

  };


  return (

    <div className="main">

      <Sidebar ref={sidebar} />

      <Content onClick={handleClick} />

    </div>

  );

}


class Sidebar extends React.Component {

  constructor(props) {

    super(props);

    this.state = { visible: true };

    this.show = this.show.bind(this);

    this.sidebar = React.createRef(null);

  }


  show() {

    if (!this.state.visible) {

      this.sidebar.current.animate(

        { flex: [1, 2], "background-color": ["teal", "red"] },

        300

      );

      this.setState({ visible: true });

    }

  }


  hide() {

    if (this.state.visible) {

      this.sidebar.current.animate(

        { flex: [2, 1], "background-color": ["red", "teal"] },

        300

      );

      this.setState({ visible: false });

    }

  }


  render() {

    return (

      <div

        ref={this.sidebar}

        className={this.state.visible ? "sidebar--visible" : "sidebar"}

        onClick={this.show}

      >

        Sidebar

      </div>

    );

  }

}


function Content({ onClick }) {

  return (

    <div className="content" onClick={onClick}>

      Content

    </div>

  );

}


ReactDOM.render(<Main />, document.getElementById("root"));

.main {

  display: flex;

  height: 100vh;

}


.sidebar {

  flex: 1;

  background-color: teal;

}


.sidebar--visible {

  flex: 2;

  background-color: red;

}


.content {

  flex: 7;

  background-color: beige;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>

<div id="root"></div>


查看完整回答
反對 回復(fù) 2022-12-22
  • 1 回答
  • 0 關(guān)注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號