hoverOn和hoverOff不起作用-if语句-React

时间:2018-11-18 03:50:45

标签: javascript reactjs jsx

从第75行开始,我试图在if语句中通过返回所有span元素来添加它们,但这给了我一个错误。

它期待一个“,”,并且不确定要添加什么。将整个元素范围包裹在字符串中?

https://github.com/cjl85/bloc-jams-react/blob/master/src/components/Album.js

第84行是给我错误的原因。 return语句应以逗号开头。

      {this.state.album.songs.map((song, index) =>
    <tr className="song"
      key={index}
        onClick={() => this.handleSongClick(song)}
          onMouseEnter={() => this.hoverOn(song)}
            onMouseLeave = {() => this.hoverOff(song)}>
    <td>
    if{(this.state.hoverOn === song)} {
          {return <span className = "ion-md-play"></span>)};
    }}
    // {(this.state.hoverOn === song) ? (<span className="ion-md-play"></span>) :
    //  (this.state.isPlaying === true && this.state.currentSong === song) ? (<span className="ion-md-pause"></span>) :
    //  (this.state.isPlaying !== true && this.state.currentSong === song) ? (<span className="ion-md-play"></span>) : null }
    </td>

1 个答案:

答案 0 :(得分:0)

你可以这样写

const { isHovered, isPlaying, currentSong } = this.state
return(
 /* Code Before */
 {isHovered === song ? (<span className="ion-md-play"/>) : null}
 {(isPlaying === true && currentSong === song) ? (<span className="ion-md-pause"/>) : null }
 {(isPlaying !== true && currentSong === song) ? (<span className="ion-md-play"/>) : null }
)

您可以再次检查吗?

但是我在您的代码上看到isHovered状态为func,所以您可以检查它