背景:
GridPreview在App中接受“长度和宽度”,用于在for loop中呈现子元素GridTile。
怎么了,
不应用GridTile组件中的样式。
vscode 0错误,0警告。Webpack编译成功。浏览器控制台0错误。
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},
${Math.random() * 100 + 100},
${Math.random() * 100 + 100}
);
justify-self: stretch;
align-self: stretch;
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"));发布于 2020-05-05 08:56:29
styled-jsx似乎只能接收简单的字符串或表达式。
请参阅:https://github.com/zeit/styled-jsx/issues/595
function GridTile({
indexOfW,
indexOfH,
}: {
indexOfW: number;
indexOfH: number;
}) {
const color = `rgb(
${Math.random() * 100 + 100},
${Math.random() * 100 + 100},
${Math.random() * 100 + 100}
)`;
return (
<div className={`tile ${indexOfW}-${indexOfH}`}>
{`${indexOfW}-${indexOfH}`}
<style jsx>{`
.tile {
background-color: ${color};
justify-self: stretch;
align-self: stretch;
grid-column: ${indexOfH + 1};
grid-row: ${indexOfW + 1};
}
`}</style>
</div>
);
}将background-color从<style jsx>中移除是可行的。
发布于 2020-05-05 06:26:05
我不知道如何使用<style jsx>,但您可以使用内联样式,如下所示:
let container = {
height: '800px',
display: 'grid',
gridTemplateRows: `repeat(${w}, 1fr)`,
gridTemplateColumns: `repeat(${h}, 1fr)`
}
return (
<div style={container}>
{tiles}
</div>
);编辑:
在分析了代码之后,我认为这就是问题所在:
background-color: rgb(
${Math.random() * 100 + 100}px,
${Math.random() * 100 + 100}px,
${Math.random() * 100 + 100}px
);您正在指定rgb()的值(以像素为单位)。
发布于 2020-05-05 09:20:19
不应用GridTile组件中的样式。
vscode 0错误,0警告。Webpack编译成功。浏览器控制台0错误。
https://stackoverflow.com/questions/61606846
复制相似问题