Javascript 在内联样式中使用状态变量

Javascript 在内联样式中使用状态变量,javascript,css,reactjs,user-interface,frontend,Javascript,Css,Reactjs,User Interface,Frontend,我希望在内联样式中使用状态变量,如下所示: const styles = { progressText1: { fontSize: this.state.text1Size } }; constructor(props, context) { super(props, context); this.state = { text1Size: "300%" }; }; …以便我可以在调整窗口大小时重置它。我得到错误“未定义没有属性”。有人知道怎么回

我希望在内联样式中使用状态变量,如下所示:

const styles = {
progressText1: {
    fontSize: this.state.text1Size
  }
};

constructor(props, context) {
    super(props, context);
    this.state = {
      text1Size: "300%"
    };
};
…以便我可以在调整窗口大小时重置它。我得到错误“未定义没有属性”。有人知道怎么回事吗


谢谢

返回之前,将
常量样式
移动到渲染函数中

可能是这样的:

render() {
  const styles = {
    progressText1: {
      fontSize: this.state.text1Size
    }
  }; 
  return (
    <div style={styles.progressText1}></div>
  )  
}
render(){
常量样式={
progressText1:{
fontSize:this.state.text1Size
}
}; 
返回(
)  
}

您可能必须在渲染中定义它。请包括您尝试使用此状态变量的整个类和位置当前,除了在构造函数中初始化text1Size,并使用它在progressText1中设置fontSize外,我还没有尝试更改text1Size。这有帮助吗?
这个.state
可能还不存在。这就是它未定义的原因。将
const styles
移动到
render
函数中,它就会工作。您使用的是类还是
create
样式。需要更多信息,否则根据您发布的内容,您首先应该会收到语法错误。哦,我不知道您以前使用过它(由@A.Lau在上面的评论对话中提到,对话很长,所以我一开始看不到)。然而,我只是想帮忙!