简体   繁体   English

如何在ReactJS中为li元素添加活动的类/状态?

[英]How to add active class/state for li element in ReactJS?

I'm struggling with one issue all day long... I have list of books and after click user can see details of each books. 我整天都在为一个问题而苦苦挣扎……我拥有书籍清单,点击后用户可以看到每本书的详细信息。 I want to add background-color when element li is clicked (something like an active class). 我想在单击元素li时添加background-color (类似于active类)。 I use Redux and React and have no idea what is the best approach to create this feature. 我使用Redux和React,却不知道创建此功能的最佳方法是什么。 Please, give me some hints because I'm getting discouraged to React environment... 请给我一些提示,因为我不鼓励使用React环境...

book-list.js book-list.js

class BookList extends Component {
    renderList(){
        return this.props.books.map((book) => {
            return (
                <li key={book.title} 
                    className="list-group-item" 
                    onClick={() => this.props.selectBook(book)}>
                    {book.title}
                </li>
            )
        })
    }

render () {
    return (
        <div>
            <h3 className="m-t-2 m-b-1">Books to read:</h3>
            <ul className="list-group col-sm-4">
                {this.renderList()}
            </ul>
        </div>
    );
 } 
}

function mapStateToProps (state) {
    return {
        books: state.books
    };
 }

function mapDispatchToProps(dispatch) {
    return bindActionCreators({ selectBook: selectBook }, dispatch)
}

export default connect (mapStateToProps, mapDispatchToProps)(BookList); 

reducer_active_book.js reducer_active_book.js

export default function(state = null, action) {
    switch(action.type) {
    case 'BOOK_SELECTED':
        return action.payload;
    }
        return state;
}

actions.js actions.js

export function selectBook (book) {
    return {
        type: 'BOOK_SELECTED',
        payload: book
    }
}

You can use classnames package https://github.com/JedWatson/classnames 您可以使用classnameshttps://github.com/JedWatson/classnames

import classNames from 'classnames';

return this.props.books.map((book) => {
        return (
            <li key={book.title} 
                className={classnames({'list-group-item':true, 'active': book.active})} 
                onClick={() => this.props.selectBook(book)}>
                {book.title}
            </li>
        )
    })

active class will affect only if book.active is true 仅当book.active为true时,活动类才会影响

//EDIT you have to pass reducer_active_book property from your state through mapStateToProps //编辑,您必须通过mapStateToProps从您的状态传递reducer_active_book属性

function mapStateToProps (state) {
return {
    books: state.books,
    reducer_active_book: state.reducer_active_book,
};

} }

Now in render you can use this. 现在可以在渲染中使用它。

return this.props.books.map((book) => {
        return (
            <li key={book.title} 
                className={classnames({'list-group-item':true, 'active': this.props.reducer_active_book.id === book.id})} 
                onClick={() => this.props.selectBook(book)}>
                {book.title}
            </li>
        )
    })
<li key={book.title} 
    className={"list-group-item " + (book.flag ? 'active-class' : 'inactive-class')}
    onClick={() => this.props.selectBook(book)}>
    {book.title}
</li>

Try setting a 'flag' when one book is selected, and conditionally add style classes(active-class/inactive-class) accordingly 选择一本书时尝试设置“标志”,并有条件地添加样式类(活动类/非活动类)

In Book List Component 在图书清单组件中

function mapStateToProps(state){

  return {
       books:state.books,
       ActiveBook:state.ActiveBook
  };
}



renderList(){

 console.log(this.props.books);
  return this.props.books.map( (book,idx)=>{

        return <li
         onClick={ ()=>this.props.selectBook(book) }
         className= {"list-group-item  " + ( this.props.ActiveBook ? ( this.props.ActiveBook.title===book.title ? "active" : "") : '') }  key={idx}>{book.title}</li>
   } );
}

I have done correctly using this. 我已经正确使用了。

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

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