0
我是React新手,想知道是否有某種方法可以直接使用某個組件的名稱,ID,密鑰,任何東西來操作。通過名稱或其他東西獲取React.js組件
例如,我創建簡單的遊戲,並有很多「細胞」。 Cell有state.cellState
的enabled
,disabled
或active
。
一旦單元格被點擊,它就會變成active
並在Enter
被按下之後 - disabled
。
這就是要點 - 是否有可能獲得活動的細胞並使用它?
我正在使用Electron,Enter
等快捷鍵已經工作。
編輯 - 下面是一些代碼
class Main extends React.Component {
render() {
let rows = [];
categories.forEach(function (category, i) {
rows.push(<Row categoryName={category} rowNo={i + 1} key={category}/>);
});
return (
<div className="clearfix" id="main">
{rows}
</div>
);
}
let Row = React.createClass({
render: function() {
let notes = [],
categoryId = this.props.rowNo;
bonuses.forEach(function (bonus, i) {
let id = 'cell_' + categoryId.toString() + (i + 1).toString();
notes.push(<NoteCell bonus={bonus} id={id} key={id}/>);
});
return (
<div className="row clearfix">
<CategoryCell categoryName={this.props.categoryName}/>
{notes}
</div>
)
}
});
class CategoryCell extends React.Component {
render() {
return (
<div className="cell category">
<h2>
{this.props.categoryName}
</h2>
</div>
)
}
}
let NoteCell = React.createClass({
getInitialState: function() {
return {active: true}
},
render: function() {
return (
<div className="cell tune" id={this.props.id} onClick={this.handleClick}>
<div className="tune-container">
<img src="./static/note.png" className="note"/>
</div>
<h3 className="value">{this.props.bonus}</h3>
</div>
)
}
});
所以部件initialy有enabled
狀態並dinamically創建。 我如何獲得當前活動單元格並使用它? 對格式化和ES2015 mixin抱歉。
感謝您的回答,但我不太明白。請看看我上面添加的代碼。 – Dmitry
這是某種'猜曲'遊戲。玩家選擇單元格,聽取midi-song並嘗試理解它是什麼歌曲。只要他明白他應該按'空格鍵',並且[最重要的]單元格被禁用。這是我無法理解的一點!我如何通過快捷方式更改一些'NoteCell'狀態?像'NoteCell [23] .setState(.....)'Smth' – Dmitry
我假設一次只能監聽1個單元。所以,只要您將狀態設置爲活動狀態,即可開始播放音樂。一旦用戶點擊空格鍵,您需要在「handleActiveItem」中將狀態更改爲禁用狀態。爲此,維護Main Component中的狀態並添加一個全局事件偵聽器,當按下空格鍵時調用一個函數。該功能會將狀態設置爲禁用狀態,並呈現更新後的用戶界面。檢測全局空格鍵是棘手的。閱讀關於它[這裏](https://github.com/facebook/react/issues/285) – Rubbal