简体   繁体   English

将 react class 组件转换为功能组件,完美渲染但功能丢失?

[英]Converting react class component to functional component, rendering perfectly but functionality lost?

I am making a small audio player application and attempting to convert my react class component into a functional component.我正在制作一个小型音频播放器应用程序,并尝试将我的 react class 组件转换为功能组件。

The original code for the class component is here: class 组件的原始代码在这里:

class Player extends React.Component {

    constructor(){
    super()

    this.state = {
        tracks: [
          {title: "first track", data: track1},
          {title: "second track", data: track2},
          {title: "third track", data: track3},
        ],
        currentTrack: 0,
    }

    }

     changeData(trackIndex){
      this.setState({currentTrack: trackIndex})
    }
  
    render(){
        return <div style={{color: this.props.color}}>
            <h1>Player</h1>
           <AudioPlayer
            src= {this.state.tracks[this.state.currentTrack].data}
            />
            <div style={{color: "green"}}>
                <ul>
                    {this.state.tracks.map((trackElem, index) => (
                       <li onClick={()=> this.changeData(index)}>{trackElem.title}</li>
                    ))}
                </ul>
            </div>
        </div>
    }
}

I have attempted the conversion below:我尝试了以下转换:

const Player2 = () => {

   const Items = [
          {title: "first track", data: track1},
          {title: "second track", data: track2},
          {title: "third track", data: track3},
    ]; 

    const [activeTrack, setActiveTrack] = useState(0);

    function ChangeData(trackIndex) {
        setActiveTrack({activeTrack : trackIndex});
    }

    return (
        <div >
           <h1>Player</h1>
           <AudioPlayer
            src= {activeTrack.data}
            />
            <div >
                <ul>
                    {Items.map((trackElem, index) => (
                       <li onClick={()=> ChangeData(index)}>{trackElem.title}</li>
                    ))}
                </ul>
            </div>
        </div>
    )
}

Aside from removing some of the props that are associated with styling the logic is basically the same.除了删除一些与样式相关的道具之外,逻辑基本相同。 The only difference is the handling of the current track, I have attempted to represent this by using states.唯一的区别是当前轨道的处理,我试图通过使用状态来表示这一点。

When rendering the object to a different page it appears to the screen completely fine, the only issue is that the tracks themselves no longer trigger a response in the audio player.当将 object 渲染到不同的页面时,它在屏幕上看起来完全正常,唯一的问题是轨道本身不再触发音频播放器中的响应。

Would really appreciate it if someone could identify anything I've missed in making the conversion?如果有人能找出我在转换过程中遗漏的任何东西,真的会很感激吗?

Here:这里:

const [activeTrack, setActiveTrack] = useState(0);

So activeTrack is a number in state initially.所以activeTrack最初是 state 中的一个数字 But then you do但是你这样做了

src= {activeTrack.data}

which doesn't make sense - the number doesn't have a data property.这没有任何意义 - 该数字没有data属性。 Another problem is另一个问题是

setActiveTrack({activeTrack : trackIndex});

Now you're setting the active track to an object with an activeTrack property, which is different from the above two formats as well.现在您将活动轨道设置为具有activeTrack属性的 object,这也与上述两种格式不同。

Pick one way to represent, set, and retrieve state throughout the component, such as the index:选择一种方法来表示、设置和检索整个组件中的 state,例如索引:

function ChangeData(trackIndex) {
    setActiveTrack(trackIndex);
}
<AudioPlayer
    src={Items[activeTrack].data}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM