我正在使用React.js構建我的投資組合。在一節中,我將四個組件放在一個網格中。我想要做的是當單擊一個組件時,將一個css類添加到此組件的同級中,以便減少其不透明度並保留單擊的組件。在jQuery中,它會像$('。component')。on('click',function(){$(this).siblings.addClass('fadeAway')})。我怎樣才能達到這個效果?這裏是我的代碼,預先感謝任何和所有的幫助!當組件在React中單擊時將類添加到同級
class Parent extends Component{
constructor(){
super();
this.state = {fadeAway: false}
this.handleClick = this.handleClick.bind(this)
}
handleClick(){
//Add class to siblings
}
render(){
const array = ["Hello", "Hi", "How's it going", "Good Times"]
return(
array.map(function(obj, index){
<Child text={obj} key={index} onClick={() => this.handleClick} />
})
)
}
}
這個解決方案實際上幫助最大。起初,它對我所做的事情似乎有點強大,但你真的解釋得很清楚,現在對我來說是完全合理的。我不得不稍微調整一下handleClick函數,並且不需要使用JSON.parse,但總體概念起作用,並且我實現了我的最終目標。謝謝!! @furkle –
@IanSpringer很高興爲您服務! – furkle