Answer the question
In order to leave comments, you need to log in
How to use querySelector in React?
You need to take the text from the div in JSX, I wrote the following code below the function with JSX:
import React from 'react';
import './App.css'
function App() {
return(
<div className='App'>
<h3 className='text'>Text1</h3>
<h3>Text: {text}</h3>
</div>
);
}
const text = document.querySelector('.text');
export default App;
Answer the question
In order to leave comments, you need to log in
there's no point in using react if you're accessing the content via .querySelector
a hook instead, useState
store your value in it and use the variable wherever you want. If necessary, you can change this value.
An example of creation and use.
If you need to change, just write
yes and it would not hurt to read the documentation of the hooks section.
const [ text, setText ] = React.useState('Text1');
<h3 className='text'>{text}</h3>
setText('новый текст');
You can do this:
https://stackoverflow.com/questions/61634308/alter...
But this is not a common way to use react. Try to avoid this approach whenever possible.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question