我正在基於動態列表提供API s'內容,並且我試圖在每個li上應用mouserEnter。通過切換每個列表項目中的內容來結果該事件。該事件正在工作,但它一次切換所有列表項中的內容,但我希望它僅切換與正在接收mouseEnter的列表項匹配的內容。React JS - 動態列表中的事件處理程序
import _ from 'lodash';
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
export default class Dribbble extends React.Component {
constructor(props) {
super(props);
this.state = {
work: [],
hover: false
};
this.handleMouseEnter = this.handleMouseEnter.bind(this);
this.handleMouseLeave = this.handleMouseLeave.bind(this);
}
handleMouseEnter(){
this.setState({ hover: true })
}
handleMouseLeave(){
this.setState({ hover: false })
}
componentDidMount() {
this.ShotList();
}
ShotList() {
return $.getJSON('https://api.dribbble.com/v1/shots?per_page=3&access_token=41ff524ebca5e8d0bf5d6f9f2c611c1b0d224a1975ce37579326872c1e7900b4&callback=?')
.then((resp) => {
this.setState({ work: resp.data.reverse() });
});
}
render() {
const works = this.state.work.map((val, i) => {
return <li key={i} className="box"
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
>
{!this.state.hover ?
<div>
<img className="cover" src={val.images.normal} />
<div className="bar">
<h2>{val.title}</h2>
<span>{val.views_count}</span>
<i className="fa fa-eye fa-2x" aria-hidden="true"></i>
</div>
</div>
: null}
{this.state.hover ?
<div>
<h3>{val.user.name}</h3>
<img className="avatar img-circle" src={val.user.avatar_url}/>
<p>{val.description}</p>
</div>
:
null
}
</li>
});
return <ul>{works}</ul>
}
}
這裏是我的代碼:
謝謝,但你能解釋我更好嗎?你的例子與我的有點不同。請給我更多的線索。 – claudiobitar
我是React JS的初學者 – claudiobitar
不同之處在於@Purgatory並不只是跟蹤是否有東西被徘徊,他們正在追蹤被徘徊的特定「工作」的ID。這樣,當你映射數組時,你檢查每一個id是否與id相匹配,然後根據這個id做出決定。 – aherriot