如何从React中访问div根元素?

huangapple go评论60阅读模式
英文:

How do I access the div root body from the react?

问题

以下是您要翻译的内容:

I have such a html project structure:

<div id="root">
   <div class="main-c container-fluid">
   </div>
   ...
<div>

My index.tsx:

import './index.css';
const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <React.StrictMode>
    <HashRouter>
      <App />
    </HashRouter>
  </React.StrictMode>
);

app:

import "bootstrap/dist/css/bootstrap.min.css"
function App() { 
return (
    <Container className="main-c" fluid>
       ...
    </Container>
  );
}

And I don't understand how to access the backgroud-color field from root?

That is, if I open the page elements in the browser, it shows that root has such a body css:

body {
    margin: 0;
    font-family: var(--bs-body-font-family);
    font-size: var(--bs-body-font-size);
    font-weight: var(--bs-body-font-weight);
    line-height: var(--bs-body-line-height);
    color: var(--bs-body-color);
    text-align: var(--bs-body-text-align);
    background-color: var(--bs-body-bg);
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

And here the background color has the color #fff, and I want to make #fff0. But how to access this field?

I tried:

index.css:

:root{
  background-color: #fff0;
}

But nothing happens. How to access this field?

如何从React中访问div根元素?

I have tried all the options below:

body {
  background-color: #fff0;
}

#root{
  background:#fff0;
}

But none of them works. I.e.

如何从React中访问div根元素?

color is still white.

英文:

I have such a html project structure:


&lt;div id=&quot;root&quot;&gt;
   &lt;div class=&quot;main-c container-fluid&gt;
   &lt;/div&gt;
   ...
&lt;div&gt;

My index.tsx:

import &#39;./index.css&#39;;
const root = ReactDOM.createRoot(
  document.getElementById(&#39;root&#39;) as HTMLElement
);
root.render(
  &lt;React.StrictMode&gt;
    &lt;HashRouter&gt;
      &lt;App /&gt;
    &lt;/HashRouter&gt;
  &lt;/React.StrictMode&gt;
);

app:

import &quot;bootstrap/dist/css/bootstrap.min.css&quot;
function App() { 
return (
    &lt;Container className=&quot;main-c&quot; fluid&gt;
       ...
    &lt;/Container&gt;
  );
}

And I don't understand how to access the backgroud-color field from root?

That is, if i open the page elements in the browser, it shows that root has such a body css:

body {
    margin: 0;
    font-family: var(--bs-body-font-family);
    font-size: var(--bs-body-font-size);
    font-weight: var(--bs-body-font-weight);
    line-height: var(--bs-body-line-height);
    color: var(--bs-body-color);
    text-align: var(--bs-body-text-align);
    background-color: var(--bs-body-bg);
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

And here the background color has the color #fff, and I want to make #fff0. But how to access this field?

i tried:

index.css:

:root{
  background-color: #fff0;
}

But nothing happens. How to access this field?

如何从React中访问div根元素?

I have tried all the options below:

body {
  background-color: #fff0;
}

#root{
  background:#fff0;
}

But none of them works. I.e.

如何从React中访问div根元素?

color is still white.

答案1

得分: 3

if changing body color is the outcome you are looking for, this would work:

in src/index.css

body {
  background-color: #fff0;
}
英文:

if changing body color is the outcome you are looking for, this would work:

in src/index.css

body {
  background-color: #fff0;
}

答案2

得分: 1

在React中,你可以使用document对象访问你的应用程序的根/主体div元素。以下是一个示例:

获取根/主体元素

const rootElement = document.getElementById('root');

// 更新根/主体元素的背景颜色

rootElement.style.backgroundColor = 'red';

在上面的示例中,我们使用document对象的getElementById方法来检索应用程序的根/主体元素,然后使用style属性将其背景颜色更新为红色。

然而,一般不建议直接在React中操作DOM,因为这可能导致意外行为并使代码难以维护。相反,你应该使用React的基于组件的方法和状态管理来更新UI。

或者使用CSS

要在CSS中选择根/主体元素,你可以使用body选择器:

如果你想专门定位根div元素(通常用作React应用程序的根元素),你可以使用它的id选择器:

#root {
  /* 这里放你的样式 */
}
英文:

In React, you can access the root/body div element of your application using the document object. Here's an example:

Get the root/body element


const rootElement = document.getElementById(&#39;root&#39;);

// Update the background color of the root/body element


rootElement.style.backgroundColor = &#39;red&#39;;

In the above example, we use the getElementById method of the document object to retrieve the root/body element of the application, and then update its background color to red using the style property.

However, it's generally not recommended to directly manipulate the DOM in React, as it can lead to unexpected behavior and make your code harder to maintain. Instead, you should use React's component-based approach and state management to update the UI.

Or using CSS

To select the root/body element in CSS, you can use the body selector:

If you want to target the root div element specifically (which is often used as the root element for React applications), you can use its id selector:


#root {
/* your styles here */
}

答案3

得分: 1

要访问一个元素,你需要在 index.css 中使用 # 选择器:

#root{
   background:#fff0;
}

你也可以在 index.js 中访问一个元素并更改它的样式:

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);
rootElement.style.background = "#fff0";

编辑:

浏览器总是应用最后的 CSS 样式,要覆盖 bootstrap 的 CSS 样式,你需要在 bootstrap 文件之后导入一个 custom css file

import "bootstrap/dist/css/bootstrap.min.css";
import "./customCssfile.css";

function App() { 
return (
    <Container className="main-c" fluid>
       ...
    </Container>
  );
}

另一个选项是使用 !important,当你使用 !important 时,浏览器会忽略在它之后出现的样式!

英文:

for access an element by id you need # selector on index.css :

#root{
   background:#fff0;
}

also you can access an element in index.js and change it's style :

const rootElement = document.getElementById(&quot;root&quot;);
const root = createRoot(rootElement);
rootElement.style.background = &quot;#fff0&quot;;

Edit :

browser always apply last css, for override a bootstrap css style you need to import custom css file after bootstrap file :

import &quot;bootstrap/dist/css/bootstrap.min.css&quot;;
import &quot;./customCssfile.css&quot;;

function App() { 
return (
    &lt;Container className=&quot;main-c&quot; fluid&gt;
       ...
    &lt;/Container&gt;
  );
}

and another option is using of !important,
when you use !important, browser ignore styles that come after it!

答案4

得分: 1

如果你要访问根 div 元素,有两种解决方法可以尝试。

第一种解决方法是将另一个 div 用作实际的根。

import React from 'react';
import ReactDOM from 'react-dom';

const App = () => {
  return (
    <div id="theFakeRoot" style={{ "background-color": "..." }}>
      {/* Your app code goes here */}
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById('root'));

第二种解决方法是使用 document.querySelector 将根 div 存储在一个常量变量中,并在各个组件之间共享它。如果你需要从多个组件中访问根 div,这种方法很有用。

import React, { useRef, useEffect } from 'react';

export const rootDiv = document.querySelector('#root');

const App = () => {
  rootDiv.style.backgroundColor = "...";
  return (
    <>
      {/* Your app code goes here */}
    </>
  );
};

export default App;
英文:

If you're trying to access the root div element, there are two solutions you can try.

The first solution is to use another div as the actual root.

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom&#39;;

const App = () =&gt; {
  return (
    &lt;div id=&quot;theFakeRoot&quot; style={{&quot;background-color&quot;:&quot;...&quot;}}&gt;
      {/* Your app code goes here */}
    &lt;/div&gt;
  );
};

ReactDOM.render(&lt;App /&gt;, document.getElementById(&#39;root&#39;));

The second solution is to store the root div using document.querySelector in a constant variable and share it across your components. This approach is useful if you need to access the root div from multiple components.

import React, { useRef, useEffect } from &#39;react&#39;;

export const rootDiv = document.querySelector(&#39;#root&#39;);

const App = () =&gt; {

rootDiv.style.backgroundColor = &quot;...&quot;;

  return (
    &lt;&gt;
      {/* Your app code goes here */}
    &lt;/&gt;
  );
};

export default App;

答案5

得分: 0

尝试这样做:

div#root{
   background:#fff0 !important;
}
英文:

Try this:

div#root{
   background:#fff0 !important;
}

huangapple
  • 本文由 发表于 2023年2月26日 23:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定