【问题标题】:mutable styles object for inline-style react components内联样式反应组件的可变样式对象
【发布时间】:2017-11-08 02:49:54
【问题描述】:

我有一个样式 obj

let styles = {
  step_div:{
    height:'150px',
  } 
}

我想渲染一些 div 对象,每个对象都有自己的颜色

class Layout extends React.Component{
  constructor(props) {
    super(props);
    this.state={
      steps_array:[
        {title:'Intro',
          types:['Gate Keeper', 'Target Prospect'],
          color:'blue'},
        {title:'Grab Attention',
          types:['Name Drop', 'Product Value'],
          color:'yellow'},
        {title:'Disqualify Statement',
          types:[],
          color:'yellow'}]

遍历数组,给样式添加边框颜色会报错

  make_structure(){
    let rows = []
    let data = this.state.steps_array
    data.forEach((i, key)=>{
      let style = styles.step_div
      style.border="1px solid "+i.color //each items color property
      let row = (
        <Row>
          <div  style={style}>
            {i.title}
          </div>
        </Row>
      )
      rows.push(row)
    })
    return rows
  }

错误

TypeError: Cannot assign to read only property 'border' of object '#<Object>'

【问题讨论】:

  • 使用 Object.assign()
  • 你有 jsfiddle 还是 plunker。
  • 你能显示styles对象吗?
  • 谢谢迪内什!有效

标签: javascript html css reactjs inline


【解决方案1】:

你可以这样试试。

let style = {...styles.step_div,border:'1px solid'+i.color};

【讨论】:

  • 让 style = {{...styles.step_div,border:'1px solid'+i.color}};
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 2020-07-15
  • 2020-09-05
  • 2020-10-27
  • 2020-07-09
  • 1970-01-01
相关资源
最近更新 更多