简体   繁体   English

我认为这是另一个React / JS上下文问题,如何提取此变量?

[英]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,你可以把它送到handleEnterhandleLeave

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 />部件,通过它的轨道,然后通过handleEnterhandleLeave作为道具。

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.

相关问题 此Javascript代码存在问题(我认为是执行上下文) - Issue with this Javascript code (I think with the execution context) 如何使用上下文 API 删除 React js 中的列表 - How do I delete a list in React js using Context API 与PHP相比,我如何看待JavaScript中的成员变量 - How do I think about member variable in JavaScript in comparison to PHP 我如何通过 React JS 中的变量 map - How do I map through a variable in React JS 我如何 go 到 function 中的另一个页面反应 js - how do i go to another page in function react js React.js如何在另一个组件中使用一个组件? - React.js How do I use a component in another component? 如何防止 React 中的冲突事件(我认为)? - How to prevent conflicting events (I think) in React? 如何从另一个站点加载图像并保存在我的站点中? 碰到CORS问题(我认为) - How to load an image from another site and save in my site? Hitting a CORS issue (I think) 如何从 js 文件上的一个 function 获取变量并将其用于另一个 js 文件上的另一个 function? - How do I get a variable from one function on a js file and use it for another function on another js file? 回溯问题(我认为吗?)-Javascript - Backtracking issue (I think?) - Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM