javascript - 在条件下将样式对象显示从“无”更改为“弹性”
问题描述
我有一个想要隐藏的组件,直到我在我的应用程序中加载某些内容。目前我正在使用样式对象来设置组件的样式。我想在运行函数时将 styles={display:'none'} 更改为 styles={display:'flex'} 。目前我正在尝试更改样式对象,然后将其传递给语音组件,但它不会完全隐藏按钮。我只能让他们的样式改变到它们作为灰色背景存在的程度。有人可以帮忙吗?
showAudio = () =>{
this.setState({showBtn: true})
console.log("show buttons")
}
audioStyles = () =>{
if(this.state.showBtn===false){
var styles={
container: {
transform: 'translate(0%, 0%)',
zIndex: '4',
width: '10%',
display: 'none',
margin: 'auto'
},
play: {
button: {
width: '2em',
height: '2em',
cursor: 'pointer',
pointerEvents: 'none',
outline: 'hidden',
backgroundColor: 'white',
border: 'hidden',
display: 'hidden',
borderRadius: 6
}
},
pause: {
button: {
width: '2em',
height: '2em',
cursor: 'pointer',
pointerEvents: 'none',
outline: 'none',
backgroundColor: 'white',
border: 'none',
display: 'hidden',
borderRadius: 6
},
},
resume: {
button: {
width: '2em',
height: '2em',
cursor: 'pointer',
pointerEvents: 'none',
outline: 'none',
backgroundColor: 'white',
border: 'none',
display: 'hidden',
borderRadius: 6
},
}
};
}else{
var styles={
container: {
transform: 'translate(0%, 0%)',
zIndex: '4',
width: '10%',
display: 'flex',
margin: 'auto'
},
play: {
button: {
width: '2em',
height: '2em',
cursor: 'pointer',
pointerEvents: 'none',
outline: 'none',
backgroundColor: 'white',
border: 'none',
borderRadius: 6
}
},
pause: {
button: {
width: '2em',
height: '2em',
cursor: 'pointer',
pointerEvents: 'none',
outline: 'none',
backgroundColor: 'white',
border: 'none',
borderRadius: 6
},
},
resume: {
button: {
width: '2em',
height: '2em',
cursor: 'pointer',
pointerEvents: 'none',
outline: 'none',
backgroundColor: 'white',
border: 'none',
borderRadius: 6
},
}
}
}
return styles
}
// my speech component which is being styled
<Speech id = "speechModule" text= {this.state.textToReadAloud} resume={true} pause={true} styles={this.audioStyles} rate={0.8} voice="Google UK English Female"/>
我希望从组件呈现的整个系列按钮完全隐藏。如果有人可以告诉我如何隐藏也可以工作的整个组件。
解决方案
您可以使用此模式来实现您的目标。
class SomeComponent extends React.Component {
constructor() {
super();
this.state = {
isDone: false
}
}
doSomething() {
// do something here
// after everything is done change the state isDone to true
this.setState({
isDone: true
});
}
render() {
let content = '<p>Loading...</p>';
if (this.state.isDone) {
content = '<h1>Hello world</h1>';
}
return content;
}
}
所以让我向你解释一下,首先在初始化组件时我们将默认状态设置isDone
为 false,然后render()
将触发并检查this.state.isDone
是 true 还是 false。的值content
取决于您的状态。doSomething()
然后您可以根据应用程序的需要调用方法来更改您的状态。调用后doSomething()
,render()
将再次执行,因为我们通过使用this.setState()
方法改变了组件的状态。
推荐阅读
- selenium - 第一个参数无效。必须是回调函数
- haskell - 试图返回元组
- javascript - ngrx/store 和 ngModel 导致了一些问题
- machine-learning - 测试网络输出#0:accuracy = 1 - Always- Caffe
- popup - Alfresco Activiti 工作流程:如何在按钮中显示弹出对话框(消息框)确认?
- python - 如何替换字典中输入的文本中的单词
- google-sheets - 索引和匹配以查找与特定列中的变量对齐的行标题
- python - python输出的网格格式
- java - 如何在简单的 Spring Boot 应用程序中自动装配扩展 CrudRepository 的存储库?
- ios - 为什么@EnvironmentObject 强制重新初始化视图?