3 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
<style type="text/css">
.hidden { display:none; }
</style>
render: function() {
return (
<div className={this.props.shouldHide ? 'hidden' : ''}>
This will be hidden if you set <tt>props.shouldHide</tt>
to something truthy.
</div>
);
}
// or in more modern JS and stateless react
const Example = props => <div className={props.shouldHide}/>Hello</div>

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
這是三元運(yùn)算符的另一種語法:
{ this.state.showMyComponent ? <MyComponent /> : null }
等效于:
{ this.state.showMyComponent && <MyComponent /> }
精益為何
以及其他語法 display: 'none';
<MyComponent style={this.state.showMyComponent ? {} : { display: 'none' }} />
但是,如果您使用過度display: 'none',則會(huì)導(dǎo)致DOM污染,并最終減慢您的應(yīng)用程序的速度。
添加回答
舉報(bào)