首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使图标显示在React - Ternary表达式中

使图标显示在React - Ternary表达式中
EN

Stack Overflow用户
提问于 2018-11-19 12:38:41
回答 2查看 840关注 0票数 0

我正在尝试将这行JSX添加到一个三元表达式中,这样每当我将鼠标悬停在song-number-column上时,我就可以在该列的位置弹出播放图标。

您可以在代码底部看到上下文中的JSX行。在td标签中有。

目前,它给了我一个解析错误:未终止的正则表达式并指向结束span标记。

我已经看过其他地方了,不确定如何解决这个错误。

代码语言:javascript
复制
<span className="ion-md-play"> { true ? this.state.hoverOn === <col 
id="song-number-column" /> : null </span>

////////////////////////////////////////////////////////////////////

import React, { Component } from 'react';
import albumData from './../data/albums';

class Album extends Component {
 constructor(props) {
   super(props);

 const album = albumData.find( album => {
   return album.slug === this.props.match.params.slug
 });

 this.state = {
   album: album,
   currentSong: album.songs[0],
   isPlaying: false
 };

 this.audioElement = document.createElement('audio');
 this.audioElement.src = album.songs[0].audioSrc;
 }

 play() {
  this.audioElement.play();
  this.setState({ isPlaying: true });
 }

 pause() {
  this.audioElement.pause();
  this.setState({ isPlaying: false });
 }

 setSong(song) {
  this.audioElement.src = song.audioSrc;
  this.setState({ currentSong: song });
 }

 handleSongClick(song) {
  const isSameSong = this.state.currentSong === song;
   if (this.state.isPlaying && isSameSong) {
     this.pause();
  } else {
   if (!isSameSong) { this.setSong(song); }
     this.play();
  }
 }

  hoverOn(song) {
    this.setState({isMouseEnter: song });

  };

  hoverOff(song) {
    this.setState({isMouseLeave: null})
  };

  render() {
   return (
    <section className="album">
    <section id="album-info">
    <img id="album-cover-art" src={this.state.album.albumCover} alt= . 
    {this.state.album.title}/>
     <div className="album-details">
  <h1 id="album-title">{this.state.album.title}</h1>
  <h2 className="artist">{this.state.album.artist}</h2>
  <div id="release-info">{this.state.album.releaseInfo}</div>
  </div>
  </section>
   <table id="song-list">

   <colgroup>
    <col id="song-number-column" />
    <col id="song-title-column" />
    <col id="song-duration-column" />
  </colgroup>

  <tbody>
  {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>

    <span className="ion-md-play"> { true ? this.state.hoverOn === <col id="song-number-column" /> : null </span>

    </td>
    <td key='number'   > {index + 1}  </td>
    <td key='title'    > {song.title} </td>
    <td key='duration' > {song.duration} </td>

    </tr>
  )}
  </tbody>
  </table>
  </section>
 );
}
}

export default Album;
EN

回答 2

Stack Overflow用户

发布于 2018-11-19 12:48:12

您的三元表达式格式不正确。替换

代码语言:javascript
复制
<span className="ion-md-play"> { true ? this.state.hoverOn === <col id="song-number-column" /> : null </span>

使用

代码语言:javascript
复制
<span className="ion-md-play"> { this.state.hoverOn === true ? (<col id="song-number-column" />) : null </span>

我还在jsx表达式两边添加了括号。这并不总是必要的,但jsx有一些怪癖,所以很多人说总是要用括号括起来。

票数 2
EN

Stack Overflow用户

发布于 2018-11-19 13:17:53

它的}更少了吗?

你可以试试下面的代码。

代码语言:javascript
复制
<span className="ion-md-play"> { this.state.hoverOn ? (<col id="song-number-column" />) : null } </span>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53368400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档