如何将样式添加到所有无限滚动渲染项目中?

发布于 2025-01-21 08:45:33 字数 1527 浏览 2 评论 0原文

我正在使用无限滚动,该样式仅适用于已经渲染的数组项目而不是新鲜渲染的数组项目...如何将其应用于将来的所有渲染阵列项目?我正在以数组的形式获取数据。我首先迭代数组项目,并在已经渲染的项目中添加了背景颜色和文本颜色,但是随着无限滚动的工作,它渲染了另一组数组元素,因此我如何在所有组件上应用此背景颜色和文本颜色?

const toggleModefunc = (props) => {
    if (Mymode === "light") {
        setMymode("dark")
        document.body.style.backgroundColor = "#042743";
          const elements = document.querySelectorAll(".card-body")
           elements.forEach(elems => {
             elems.style.backgroundColor = "#042743";
             elems.style.color = "#fff"
         });
    }

    else {
        setMymode("light")
        document.body.style.backgroundColor = "white";
         const elements2 = document.querySelectorAll(".card-body")
           elements2.forEach(elems => {
             elems.style.backgroundColor = "#fff";
             elems.style.color = "black"
         });
    }
}

//地图功能

{this.state.articles.map((element) => {
            return (
              <div className="col-md-4" key={element.url}>
                <Newsitem
                  title={element.title ? element.title : ""}
                  description={
                    element.description ? element.description : ""
                  }
                  imageurl={element.urlToImage}
                  newsUrl={element.url}
                  author={element.author}
                  date={element.publishedAt}
                  source={element.source.name}
                />
              </div>

I am using infinite scroll, the style was applied only to already rendered array items and not freshly rendered array items...how can I apply this to all the future rendering array items? I am fetching data in the form of an array. I first iterated array items and added background color and text color to the already rendered items but as the infinite scroll works, it rendered another set of array elements so how can I apply this background color and text color across all the components?

const toggleModefunc = (props) => {
    if (Mymode === "light") {
        setMymode("dark")
        document.body.style.backgroundColor = "#042743";
          const elements = document.querySelectorAll(".card-body")
           elements.forEach(elems => {
             elems.style.backgroundColor = "#042743";
             elems.style.color = "#fff"
         });
    }

    else {
        setMymode("light")
        document.body.style.backgroundColor = "white";
         const elements2 = document.querySelectorAll(".card-body")
           elements2.forEach(elems => {
             elems.style.backgroundColor = "#fff";
             elems.style.color = "black"
         });
    }
}

// The map function

{this.state.articles.map((element) => {
            return (
              <div className="col-md-4" key={element.url}>
                <Newsitem
                  title={element.title ? element.title : ""}
                  description={
                    element.description ? element.description : ""
                  }
                  imageurl={element.urlToImage}
                  newsUrl={element.url}
                  author={element.author}
                  date={element.publishedAt}
                  source={element.source.name}
                />
              </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的故事 2025-01-28 08:45:33

从技术上讲,您可以在React中做类似的事情,

让我们假设NewsItem是.Card-Body,而iSdark可以代表您的状态。我将假设您的状态的默认设置为light主题。

状态将根据您的按钮单击更改,这将更改数组中项目的颜色。

{
  this.state.articles.map((element) => (
    <div className="col-md-4" key={element.url}>
      <Newsitem
        className={`card-body ${isDark ? : 'dark-class' : 'fff'`}} // maybe you can add a custom css class for dark theme
    
        title={element.title ? element.title : ""}
        description={element.description ? element.description : ""}
        imageurl={element.urlToImage}
        newsUrl={element.url}
        author={element.author}
        date={element.publishedAt}
        source={element.source.name}
      />
    </div>
  ));
}

Technically you can do something like this in react

Lets just assume NewsItem is .card-body, and isDark can represent your state. I'll assume the default of your state is set to light theme.

The state will change based on your button click, which will change the color for the items within the array.

{
  this.state.articles.map((element) => (
    <div className="col-md-4" key={element.url}>
      <Newsitem
        className={`card-body ${isDark ? : 'dark-class' : 'fff'`}} // maybe you can add a custom css class for dark theme
    
        title={element.title ? element.title : ""}
        description={element.description ? element.description : ""}
        imageurl={element.urlToImage}
        newsUrl={element.url}
        author={element.author}
        date={element.publishedAt}
        source={element.source.name}
      />
    </div>
  ));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文