admin管理员组文章数量:1315785
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). I use Redux and React and have no idea what is the best approach to create this feature. Please, give me some hints because I'm getting discouraged to React environment...
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
export default function(state = null, action) {
switch(action.type) {
case 'BOOK_SELECTED':
return action.payload;
}
return state;
}
actions.js
export function selectBook (book) {
return {
type: 'BOOK_SELECTED',
payload: book
}
}
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). I use Redux and React and have no idea what is the best approach to create this feature. Please, give me some hints because I'm getting discouraged to React environment...
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
export default function(state = null, action) {
switch(action.type) {
case 'BOOK_SELECTED':
return action.payload;
}
return state;
}
actions.js
export function selectBook (book) {
return {
type: 'BOOK_SELECTED',
payload: book
}
}
Share
Improve this question
asked Apr 17, 2018 at 11:51
ItalikItalik
6963 gold badges20 silver badges37 bronze badges
1
- do you have a github link to your project? – Andrew Lam Commented Apr 17, 2018 at 13:47
3 Answers
Reset to default 3You can use classnames
package https://github./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
//EDIT you have to pass reducer_active_book property from your state through mapStateToProps
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.
本文标签: javascriptHow to add active classstate for li element in ReactJSStack Overflow
版权声明:本文标题:javascript - How to add active classstate for li element in ReactJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741961624a2407312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论