私はdivでクリックしたときに "selected"クラスを追加し、他の要素から "selected"クラスを削除するメニューナビゲーション用のReactJSアプリケーションを作成しています。 今、私は他の要素からクラスを削除する方法で立ち往生しています ここに私のコードがある他の要素をクリックしたときにReactJSのクラスを削除するには?
<pre>
**App.js**
class App extends React.Component {
constructor(){
super();
this.state = {
active : "0",
}
}
clickHandler(index){
}
render() {
let menu = this.props.menu; // menu is a array of page name e.g. ['home','about us','contact us']
let style = 0;
return (
{menu.map((menu, index) =>
List clickHandler={this.clickHandler.bind(this, index)} index={index} key={index} menu={menu}
)}
);
}
}
export default App;
** list.js **
import React from 'react';
class List extends React.Component{
constructor(props){
super(props)
this.state = {
focused : "0"
}
}
clickMe(index){
let style = "";
this.setState({focused: index});
if(this.state.focused == index){
console.log(index);
style = "selected";
}
this.props.clickHandler();
}
render(){
let style = "";
if(this.state.focused === this.props.index){
style = "selected";
}else{
style = "";
}
return li className={style} onClick={this.clickMe.bind(this, this.props.index)}>{this.props.menu} /li>
}
}
export default List;
</pre>