如何在反应中动态地将语义按钮设置为禁用:
<Button disabled>click here<Button>
我尝试用状态设置它,但它给了我一个错误
this.setState({d:'disabled'})
return (<Button {this.state.d} >click here<Button>)
不可能知道你的 Button 在底层是如何处理禁用的,但假设它像 JSX element 一样工作。
首先,JSX 元素只是带有一组参数(props)的函数。所以你仍然需要给它一个disabled:boolean。如下所示,您需要提供名称和值。您对 {this.state.d} 的尝试仅给出值 true/false。请查看下面的代码片段,了解如何做到这一点。要么是显式的,要么是给它一个命名变量,最后是通过展开一个对象。
class HelloWorldComponent extends React.Component {
constructor(){
super();
this.state = {
disabled: true
}
}
render() {
const disabled = this.state.disabled; //Pull out the value to a named variable
return (
<div>
<button disabled={false}>Button1</button>
<button disabled>Button2</button>
<button {...this.state}>Button3</button>
</div>
);
}
}
React.render(
<HelloWorldComponent/>,
document.getElementById('react_example')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<div id="react_example"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)