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

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

在 React v15.5.4 中將自定義屬性設(shè)置為 DOM 元素

在 React v15.5.4 中將自定義屬性設(shè)置為 DOM 元素

郎朗坤 2022-07-21 22:11:20
目前在我的渲染方法中,我有這樣的東西<h1 customAttibute="customValue" className="classValue">This h1</h1>這是在我的瀏覽器中呈現(xiàn)的<h1 class="classValue">This h1</h1>這是因?yàn)?React 15 似乎不支持自定義屬性令人討厭的是,在 React v16 中,這個(gè)問(wèn)題似乎已經(jīng)解決,但在這個(gè)階段升級(jí)到 React 16 對(duì)我來(lái)說(shuō)不是一個(gè)選擇 - https://reactjs.org/blog/2017/09/08/dom-attributes-in-react- 16.html無(wú)論如何我可以為我的元素添加一個(gè)自定義屬性,也許是 setAttribute ?如果有人能指出我如何做到這一點(diǎn)的正確方向或一些令人驚嘆的文檔
查看完整描述

2 回答

?
侃侃爾雅

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

您可以使用回調(diào)參考來(lái)做到這一點(diǎn)。


class MyComponent extends React.Component {

  constructor(props) {

    super(props);


    this.element = null;

    this.setElementRef = (element) => {

      this.element = element;

    };

  }


  componentDidMount() {

    this.element.setAttribute("customattribute", "foo bar");

  }


  render() {

    return (

      <div ref={this.setElementRef} className="classValue">

        Hello world!

      </div>

    );

  }

}

您可以在 React 的文檔中閱讀更多相關(guān)信息,盡管其中一些信息比 React 15.5.4 更新(例如React.createRef)。


查看完整回答
反對(duì) 回復(fù) 2022-07-21
?
不負(fù)相思意

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

您可以使用ref, 來(lái)獲取對(duì) DOM 元素的setAttribute引用componenDidMount:


constructor(props) {

    super(props);

    this.myRef = React.createRef();

}


componentDidMount(){

    this.myRef.current.setAttribute('customAttibute', 'customValue')

}


render() {

    return <h1 ref={this.myRef} className="classValue">This h1</h1>;

}


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

添加回答

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