【发布时间】:2019-07-23 08:43:07
【问题描述】:
我已经设法在 React 中创建了类,并希望将随机生成的背景设置为 div 容器。名为 divStyle 的常量变量确实包含 CSS 函数 rbg() 但我只是找不到将变量从 this.state 传递给该函数的解决方案
import React from 'react';
import './ShopItem.css';
class ShopItem extends React.Component{
constructor(props){
super(props);
this.state = {
r:Math.floor(Math.random() * 256),
g:Math.floor(Math.random() * 256),
b:Math.floor(Math.random() * 256)
}
}
componentDidMount() {
console.log(this.state.r, this.state.g, this.state.b);
}
render(){
const divStyle = {
background: "rgb()"
};
return(
<div className="Item" style={divStyle} >
{console.log("test")}
{this.props.data}
</div>
);
};
};
export default ShopItem;
【问题讨论】:
标签: css reactjs react-router