[英]Conditionally rendering contents of a button
我正在做一個記憶游戲CodeSandbox ,我正在嘗試完成以下任務:
我嘗試在父div中添加“ buttonMask”類,但它覆蓋了我現有的類。 我試過顯示:父div也沒有,也沒有運氣。
其他嘗試包括向Card.js組件this.state.isHidden:true添加state屬性,並嘗試切換可見性。 我閱讀了This , SO和eddyerburgh的文檔 。 我被困在這種情況下渲染,其他一切都很好,這塊讓我
這是我發卡的條件
class Cards extends Component {
constructor(props) {
super(props);
}
render() {
const card = (
<div style={sty}>
{this.props.cardTypes ? (
this.props.cardTypes.map((item, index) => {
return (
<button style={cardStyle} value={item} key={index}>
{" "}
{item}{" "}
</button>
);
})
) : (
<div> No Cards </div>
)}
</div>
);
return <Card card={card} removeMatches={this.props.removeMatches}
/>;
}
最初,當我第一次嘗試此操作時,我將狀態傳遞給所有卡,而不僅僅是單個卡,這就是為什么我無法單獨切換它們的原因。
這是Cards.js中的原始渲染
if (this.state.hidden) {
return (
<button
key={card + index}
style={btn}
onClick={this.revealIcon}
id={card}
>
<div style={btnMask}>{card}</div>
</button>
);
} else {
return (
<button
key={card + index}
style={btn}
onClick={this.revealIcon}
id={card}
>
<div>{card}</div>
</button>
);
}
如果使用邏輯&&,則可以有條件地使用內聯呈現元素:
return(
<div>
{this.props.shouldRenderTheThing &&
<span>Conditionally rendered span</span>}
</div>
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.