[英]I think this is another React/JS context issue, how do I extract this variable?
I have an array[] of tracks that I receive from an API. 我有一个从API接收到的音轨数组[]。 I pass it to a map function which will return a track for every track in tracks.
我将其传递给地图函数,该函数将为轨道中的每个轨道返回一个轨道。 I want to export a variable (Song) specific to that track to be be processed in my event handler as such.
我想导出特定于该音轨的变量(Song),以便在事件处理程序中进行处理。 The only thing thats not working is the scope of song.
唯一不起作用的是歌曲的范围。 I cant set the state of song in my map function or the component goes into an infinite rerender loop.
我无法在地图函数中设置歌曲的状态,否则该组件将进入无限重渲染循环。
handleEnter(){
//I want to get the song into this context and play it here
this.props.mouseEnter();
}
handleLeave(){
//same for pausing
this.props.mouseLeave();
}
createTrack(track){
var song = new Audio([track.preview_url]);
return (
<div className="image" key={track.id}>
<img
className="img-circle"
src={track.album.images[0].url}
onMouseEnter={this.handleEnter.bind(this)}
onMouseLeave={this.handleLeave.bind(this)}
/>
<p className="showMe"><span>{track.name}</span></p>
</div>
);
}
getTracks(){
if(this.props.tracks) {
console.log(this.props.tracks);
return (
<div>{this.props.tracks.map(track => this.createTrack(track))}</div>
);
}
}
componentWillMount(){
this.props.fetchMessage();
}
render(){
return(
<div>{this.getTracks()}</div>
)
}
if you want to use .bind, you can send it to handleEnter
and handleLeave
. 如果你想使用.bind,你可以把它送到
handleEnter
和handleLeave
。
handleEnter( trackID ) {
// trackID available here
}
createTrack(track){
var song = new Audio([track.preview_url]);
return (
<div className="image" key={track.id}>
<img
className="img-circle"
src={track.album.images[0].url}
onMouseEnter={this.handleEnter.bind( this, track.id )}
onMouseLeave={this.handleLeave.bind( this, track.id )}
/>
<p className="showMe"><span>{track.name}</span></p>
</div>
);
}
It's typically best practice to not use .bind in react since it creates a new function on every render. 通常最好的做法是不要在反应中使用.bind,因为它会在每个渲染器上创建一个新函数。 Rather, you should create a
<Track />
component, pass it the track, then pass handleEnter
and handleLeave
as props. 相反,应该创建一个
<Track />
部件,通过它的轨道,然后通过handleEnter
和handleLeave
作为道具。
const track = ( props ) => {
let { track, handleEnter, handleLeave } = props;
const onMouseEnter = () {
handleEnter( track.id );
}
const onMouseLeave = () {
handleLeave( track.id );
}
return (
<div className="image" key={track.id}>
<img
className="img-circle"
src={track.album.images[0].url}
onMouseEnter={ onMouseEnter }
onMouseLeave={ onMouseLeave }
/>
<p className="showMe">
<span>{track.name}</span>
</p>
</div>
);
};
then in your render, you'd map like you're doing and output <Track />
pure components instead of full-on components 然后在渲染中进行映射,并输出
<Track />
纯组件而不是完整组件
Have a look at this. 看看这个。 Hopefully it will solve your problem.
希望它将解决您的问题。
handleEnter(track, e){
// you will get the full track object and use the data
this.props.mouseEnter();
}
handleLeave(track, e){
// you will get the full track object and use the data
this.props.mouseLeave();
}
componentWillMount(){
this.props.fetchMessage();
}
render(){
const createTrack = (track, index) => {
var song = new Audio([track.preview_url]);
return (
<div className="image" key={'track-'+ index}>
<img
className="img-circle"
src={track.album.images[0].url}
onMouseEnter={this.handleEnter.bind(this, track)}
onMouseLeave={this.handleLeave.bind(this,track)}
/>
<p className="showMe"><span>{track.name}</span></p>
</div>
);
}
return(
<div>{this.props.tracks ? this.props.tracks.map(createTrack) : null }</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.