K
K
konstantinst132020-07-14 17:38:02
JavaScript
konstantinst13, 2020-07-14 17:38:02

How to write an html element rendered by React to a variable?

I need to write to variables the elements that are rendered and have a className. How to do it? I am migrating my project to React and this is baffling me.

import React, {
    Component
} from 'react';

export default class Header extends Component {
    constructor() {
        super();
        let createText = document.querySelector(".create-page-text"),
            createBtn = document.querySelector(".create-page-btn"),
            headersArea = document.querySelector('.create-page-area');

    }
    render() {
        return ( 
            <header className = "header" >
                    <nav className = "nav" >
                         <input type = "text" className = "create-page-text" ></input> 
                         <button className = "create-page-btn" > Создать страницу </button> 
                    </nav> 
                <div className = "create-page-area" > < /div>
            </header>
        )
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-07-14
@konstantinst13

Using refs
https://learn-reactjs.ru/core/refs-and-the-dom
Example:

class MyComponent extends React.Component {
    constructor(props) {
      super(props);
      this.myRef = React.createRef();
    }
    render() {
      return <div className={"someClass"} ref={this.myRef} />;
    }
  }

this.myRef.current will contain a reference to an element with class someClass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question