0

该项目有以下参考,它返回一个字符串:

const left = slide.imageLeft; // introLeft


并进一步在 React Component 中渲染它。但它以字符串形式返回,styles.imageLeft并且由于 webpack 不会将其转换为相应914u923asdsajdlj1l23的捆绑类,就像不应用样式一样。

<div className={`styles.${left}`}>&nbsp;</div>


PS我确实尝试过评估,但它会丢弃2个错误。

There is an internal error in the React performance measurement code. Did not expect componentDidMount timer to start while render timer is still in progress for another instance.

ReferenceError: styles is not defined

您能否建议为 css-loader 实现动态类生成的可能方法。

4

1 回答 1

1

您必须在render()或组件定义中定义样式,如下所示

render: function(){
  var myStyle = {
    // your style rules go here
  };
  return(
    <div style={myStyle}>
      {this.props.children}
    </div>
  )
}

在某种程度上,这已经是动态的,因为您所要做的就是更改样式,它会确保组件将在更新时重新渲染

于 2017-03-04T03:24:31.760 回答