简体   繁体   English

React-切换CSS类

[英]React - toggle css class

i want to be able to toggle class "active" on/off on a single item of a list. 我希望能够在列表的单个项目上打开/关闭类“活动”。 Say we have a list of elements: 假设我们有一个元素列表:

<ul>
  <li><a href="#" className="list-item">First</a></li>
  <li><a href="#" className="list-item">Second</a></li>
  <li><a href="#" className="list-item">Third</a></li>
</ul>

and I want to be able to add class "active" on a second element on click. 并且我希望能够在点击的第二个元素上添加“活动”类。 I cannot use state for that, because it would change the classes of other 2 elements, if we add the condition there, right ? 我不能为此使用状态,因为如果在其中添加条件,它将改变其他2个元素的类,对吗?

So the solution could be to create a sub-component say <ListItem /> that could have its own state and own onclick method, that would change only his own class. 因此,解决方案可能是创建一个子组件,例如<ListItem /> ,该子组件可以具有自己的状态和自己的onclick方法,这只会更改自己的类。 But then, when we want to remove class "active" on other elements on click, this sub-component would have to dispatch method to a parent to remove class from other elements. 但是,当我们要在单击时删除其他元素上的“活动”类时,此子组件将必须向父对象分派方法以从其他元素中删除类。 It seems pretty complicated for a task of a easy kind ( you know the 10secs-in-jquery kind). 对于一个简单的任务(您知道10secs-jquery的任务)来说,这似乎非常复杂。

Does anyone have an easier solution for this ? 有人对此有更简单的解决方案吗?

It would be very nice to not have to use another big npm module for this, document.getElementsByClassName (obviously) nor refs. 不必为此使用另一个大型npm模块,即document.getElementsByClassName(显然)或refs,将是非常好的。

The correct way to approach this problem would be to have a state variable that defines which one is currently "active", ie 解决此问题的正确方法是拥有一个状态变量,该变量定义当前处于“活动”状态的变量,即

this.state = {
    active: "first" // (or "second", "third", null, etc..) 
};

Then, you can use an inline if statement like so: 然后,您可以使用内联if语句,如下所示:

<li><a href="#" className={"list-item" + (this.state.active == "first" ? " active": "")}>First</a></li>

Note that I am assuming you want to hardcode these list elements - if these elements are dynamically generated it's a simple matter of setting this.state.active to being the index/ref of the currently selected element. 请注意,我假设您要对这些列表元素进行硬编码-如果这些元素是动态生成的,只需将this.state.active设置为当前所选元素的索引/引用即可。

you can manage the selected state outside the item component and pass it as a prop. 您可以在item组件外部管理所选状态,并将其作为道具传递。
An event handler of onSelect / onClick can trigger and change the state . onSelect / onClick的事件处理程序可以触发和更改state

 const data = [1,2,3,4,5]; class List extends React.Component { constructor(props){ super(props); this.state = { selectedItem: 0 }; this.onSelect = this.onSelect.bind(this); } onSelect(id){ this.setState({ selectedItem: id }); } render() { const {selectedItem} = this.state; return ( <ul> {data.map((d, index) => { return <ListItem key={index} selected={index + 1 == selectedItem} value={d} id={d} onSelect={this.onSelect} /> })} </ul> ) } } const ListItem = (props) => { const onSelect = (e) => { props.onSelect(e.target.id); } const className = props.selected && "selected"; return <li className={className} id={props.id} onClick={onSelect}>{props.value}</li> } ReactDOM.render(<List />, document.getElementById("root")); 
 ul{ list-style: none; } li{ padding: 5px; border: 1px solid #ccc; cursor: pointer; } li.selected{ background-color: green; color: #fff; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM