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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从技术上讲,您可以在React中做类似的事情,
让我们假设NewsItem是.Card-Body,而
iSdark
可以代表您的状态。我将假设您的状态的默认设置为light
主题。状态将根据您的按钮单击更改,这将更改数组中项目的颜色。
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 tolight
theme.The state will change based on your button click, which will change the color for the items within the array.