简体   繁体   English

如何通过 Javascript 修改 CSS 模块样式

[英]How to Modify CSS Module Style via Javascript

I'm fairly new to javascript and react, but I'm diving in and creating my first react app.我对 javascript 和反应相当陌生,但我正在潜入并创建我的第一个反应应用程序。

I'm tying to modify the amount of "gridTemplateRows" that display on my screen via a variable and modify it with a new numbers based on the results from the list (I've hard coded this as a 6 right now), however I'm using CSS Modules and i can't seem to get the grid to change its row count and display properly.我想通过一个变量来修改屏幕上显示的“gridTemplateRows”的数量,并根据列表中的结果用一个新的数字来修改它(我现在已经硬编码为 6),但是我我正在使用 CSS 模块,我似乎无法让网格更改其行数并正确显示。

Component Code组件代码

import classes from "./TimeSlot.module.scss";
import AvailabilityCalendarData from "../../../../../FakeDB/AvailabilityCalendarData";

function TimeSlot() {
  const addTimeSlot = () => {
    document.getElementsByClassName(
      `${classes.time_interval}`
    ).styles.gridTemplateRows = "repeat(6, 1fr)";
  };

  const timeSlotRow = AvailabilityCalendarData.map((timeSlot) => (
    <div key={timeSlot.id}>{timeSlot.startTime}</div>
  ));

  return (
    <div className={classes.time_interval} onLoad={addTimeSlot()}>
      {timeSlotRow}
    </div>
  );
}

export default TimeSlot;

CSS CSS

.time_interval {
  grid-area: time;
  display: grid;
  grid-template-rows: repeat(17, 1fr);
  font-size: 14px;
  & > div {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    box-shadow: inset 0 1px 0 0 #eceff1;
  }
}

Just set the style :只需设置style

<div className={classes.time_interval} 
  style={{ gridTemplateRows: `repeat(${timeSlotRow.length}, 1fr)` }}
>{timeSlotRow}</div>

Or you could take a look atgrid-auto-rows .或者你可以看看grid-auto-rows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM