[英]React/Redux: On Hover of one Component, change color of all Components
让<Word />
成为一个简单的功能组件(没有状态),它需要一些道具并显示一个单词。
<Word group={1} />
<Word group={2} />
<Word group={2} />
<Word group={1} />
<Word group={2} /> //There might be many more groups etc.
在悬停其中一个<Words />
,我想突出显示(将背景颜色改为黄色或其他东西)同一组中的所有单词。 不仅仅是单词徘徊,而是单词+同一组中的所有单词。
我原本想用CSS做这件事,但这显然是不可能的。 我怎么能以最小的方式用React做这样的事情?
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor(props){
super(props);
this.state = {
currentSelectedGroup: 0,
value: 0
};
this.hoverOff = this.hoverOff.bind(this);
this.hoverOn = this.hoverOn.bind(this);
}
hoverOn(selectedGroup){
this.setState({
currentSelectedGroup: selectedGroup
});
}
hoverOff(){
this.setState({ currentSelectedGroup: 0 });
}
render(){
return(
<div>
<Word group={2}
currentSelectedGroup={this.state.currentSelectedGroup}
onMouseEnter={ ( ) => this.hoverOn(2) }
onMouseLeave = {this.hoverOff} />
<Word group={1}
currentSelectedGroup={this.state.currentSelectedGroup}
onMouseEnter={ ( ) => this.hoverOn(1) }
onMouseLeave = {this.hoverOff}
/>
<Word group={1}
currentSelectedGroup={this.state.currentSelectedGroup}
onMouseEnter={ ( ) => this.hoverOn(1) }
onMouseLeave = {this.hoverOff}
/>
<Word group={2}
currentSelectedGroup={this.state.currentSelectedGroup}
onMouseEnter={ ( ) => this.hoverOn(2) }
onMouseLeave = {this.hoverOff} />
</div>
)
}
}
const Word = (props) => {
let wordClassName = props.group == props.currentSelectedGroup ? 'highLight':'default';
return (
<div className={`${wordClassName}`}
onMouseEnter={ props.onMouseEnter }
onMouseLeave = {props.onMouseLeave}>
This is my Word Group : {props.group}
</div>);
}
export default App;
根据需要实现highLight css样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.