Languages
[Edit]
PL

React - jak pobra─ç referencj─Ö do elementu div

0 points
Created by:
telsa
442

W tym artykule chcieliby┼Ťmy pokaza─ç, jak uzyska─ç referencj─Ö do elementu div w┬áReakcie.

W poni┼╝szym przyk┼éadzie tworzymy referencj─Ö┬áza pomoc─ů hooka┬áReact.useRef, nast─Öpnie przypisujemy j─ů do w┼éa┼Ťciwo┼Ťci┬áref┬áelementu div.

Teraz, aby uzyska─ç dost─Öp do┬áreferencji┬áelementu┬áDOM, mo┼╝emy u┼╝y─ç┬áw┼éa┼Ťciwo┼Ťci┬ádivRef.current, a dost─Öp do warto┼Ťci┬ájaka si─Ö tam aktualnie znajduje uzyskamy dzi─Öki divRef.current.value.

// ONLINE-RUNNER:browser;

// Uwaga: Odkomentuj poni┼╝sze linijki podczas pracy z kompilatorem JSX:
// import React from 'react';
// import ReactDOM from 'react-dom';

const divStyle = {
    height: '20px',
    background: 'yellow',
    border: 'solid',
    borderColor: 'red'
};

const App = () => {
  const divRef = React.useRef();
  return (
    <div>
      <div ref={divRef} style={divStyle}>
        <b>My div</b>
      </div>
      <br />
      <button onClick={() => console.log(divRef.current)}>Sprawd┼║ referencj─Ö</button>
      <button onClick={() => console.log(divRef.current.innerText)}>Check innerText</button>
      <button onClick={() => console.log(divRef.current.innerHTML)}>Check innerHTML</button>
      <button onClick={() => console.log(divRef.current.outerHTML)}>Check outerHTML</button>
    </div>
  );
}

const root = document.querySelector('#root');
ReactDOM.render(<App />, root);

Alternative titles

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS (PL)

React - jak pobra─ç referencj─Ö do elementu div
Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join