V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MXXXXXS
V2EX  ›  React

styled-jsx 动态样式没有被应用到元素上

  •  
  •   MXXXXXS · 2020-05-05 14:20:51 +08:00 · 718 次点击
    这是一个创建于 1455 天前的主题,其中的信息可能已经有所发展或是发生改变。

    背景:App里的GridPreview接受"长宽", 使用for 循环渲染一系列子元素GridTile

    问题: GridTile组件内的样式没有被应用

    vscode 0 error , 0 warning. webpack 编译通过. 浏览器 0 error.

    预期: GridTile 组件内的样式正常渲染

    import React, { useState, useEffect, useCallback } from "react";
    import ReactDOM from "react-dom";
    
    function App() {
      const [gridW, setGridW] = useState<number>(5);
      const [gridH, setGridH] = useState<number>(5);
      const setGrid = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
        const current = e.currentTarget;
        switch (current.className) {
          case "setGridW": {
            setGridW(parseInt(current.value));
            break;
          }
          case "setGridH": {
            setGridH(parseInt(current.value));
            break;
          }
        }
      }, []);
      useEffect(() => {}, [gridW, gridH]);
      return (
        <>
          <div className="gridSizeSetting">
            <label htmlFor="">
              width
              <input
                type="range"
                min="1"
                max="24"
                step="1"
                value={gridW}
                className="setGridW"
                onChange={setGrid}
                name=""
                id=""
              />
            </label>
            <span>X</span>
            <label htmlFor="">
              height
              <input
                type="range"
                min="1"
                max="24"
                step="1"
                value={gridH}
                className="setGridH"
                onChange={setGrid}
                name=""
                id=""
              />
            </label>
          </div>
          <GridPreview w={gridW} h={gridH}></GridPreview>
        </>
      );
    }
    
    function GridTile({
      indexOfW,
      indexOfH,
    }: {
      indexOfW: number;
      indexOfH: number;
    }) {
      return (
        <div className={`tile ${indexOfW}-${indexOfH}`}>
          {`${indexOfW}-${indexOfH}`}
          <style jsx>{`
            div {
              background-color: rgb(
                ${Math.random() * 100 + 100}px,
                ${Math.random() * 100 + 100}px,
                ${Math.random() * 100 + 100}px
              );
              justify-self: strech;
              align-self: strech;
              grid-column: ${indexOfH + 1};
              grid-row: ${indexOfW + 1};
            }
          `}</style>
        </div>
      );
    }
    
    function GridPreview({ w, h }: { w: number; h: number }) {
      const tiles: Array<JSX.Element> = [];
      for (let indexOfW = 0; indexOfW < w; indexOfW++) {
        for (let indexOfH = 0; indexOfH < h; indexOfH++) {
          tiles.push(
            <GridTile
              key={`${indexOfW},${indexOfH}`}
              indexOfH={indexOfH}
              indexOfW={indexOfW}
            ></GridTile>
          );
        }
      }
      return (
        <div className="container">
          {tiles}
          <style jsx>{`
            .container {
              height: 800px;
              display: grid;
              grid-template-rows: repeat(${w}, 1fr);
              grid-template-columns: repeat(${h}, 1fr);
            }
          `}</style>
        </div>
      );
    }
    
    ReactDOM.render(<App />, document.querySelector("#root"));
    

    browser screenshot

    react dev tool screenshot

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5518 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:03 · PVG 16:03 · LAX 01:03 · JFK 04:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.