简体   繁体   English

react-transition-group 和 react-abc (abcjs) Midi 不能同时工作(取决于父组件的 Key)?

[英]react-transition-group and react-abc (abcjs) Midi will not work at the same time (depends on Key of parent component)?

I have a small React project where the user can add notes to a Row, and then play MIDI audio of the notes that have been added (using react-abc / abcjs ).我有一个小型 React 项目,用户可以在其中向 Row 添加音符,然后播放已添加音符的 MIDI 音频(使用react-abc / abcjs )。 The selected Notes should also animate in, using react-transition-group .选定的 Notes 也应该使用react-transition-group 进行动画处理。

My problem is that only one of these features (MIDI / animation) will work at the same time, and which one works depends on a parent components key property.我的问题是这些功能中只有一个(MIDI/动画)可以同时工作,而哪一个工作取决于父组件的key属性。 The two keys are两个键是

key={rowIndex + rowNotation}
key={rowIndex}
// where `rowNotation` is the updated data of what the MIDI should play when every new card is added

/* If `key` = `rowIndex + rowNotation`, MIDI will play when green button is pushed, but
 react-transition-group will not perform an animation when adding / removing notes

 If `key` = `rowIndex`, react-transition-group will perform enter / exit
 animations, but react-abc MIDI will NOT play
*/

I can understand why the animation would stop working - if the key updates with new data, and not just the index, the component will re-render, which means the animation cannot happen.我可以理解为什么动画会停止工作 - 如果键更新了新数据,而不仅仅是索引,组件将重新渲染,这意味着动画不会发生。

But I cannot understand why the MIDI would stop working properly when the key is just the index.但是我不明白为什么当关键只是索引时 MIDI 会停止正常工作。

It took me quite a while to even figure out that the key was causing the MIDI to stop playing.我花了很长时间才弄清楚是关键导致 MIDI 停止播放。 I've tried moving the MIDI component to different locations with no results, and I'm pretty stumped.我试过将 MIDI 组件移动到不同的位置但没有结果,我很难过。

I have created a (hopefully simple) working Sandbox of my issue with instructions to replicate: https://codesandbox.io/s/transition-group-x-abc-midi-bug-hunt-so-279pv?fontsize=14&hidenavigation=1&theme=dark我已经为我的问题创建了一个(希望很简单)工作沙箱,并附有复制说明: https : //codesandbox.io/s/transition-group-x-abc-midi-bug-hunt-so-279pv?fontsize=14&hidenavigation= 1&主题=黑暗

I have commented every main component with information, but the 2 most relevant components to my problem are:我已经用信息评论了每个主要组件,但与我的问题最相关的 2 个组件是:

Board.js : where the key can be changed to demonstrate the issue Board.js :可以更改密钥以演示问题的地方

CardRow.js : where the actual react-abc / Midi and react-transition-group components are located. CardRow.js :实际的react-abc / Midireact-transition-group组件所在的位置。

I like your example, nice work.我喜欢你的例子,干得好。

It was a big revelation when I understand how key works in React.当我了解关键在 React 中的工作原理时,这是一个很大的启示。 Let me explain you what happens here.让我解释一下这里发生了什么。

Key tells react if a component representation in the dom is reused or not. Key 告诉 react dom 中的组件表示是否被重用。 If key remain the same it will be reused.如果密钥保持不变,它将被重用。 If it changes it will be destroyed and remounted.如果它发生变化,它将被销毁并重新安装。

So when you use rowIndex as key.因此,当您使用 rowIndex 作为键时。 It remains the same and the animation works.它保持不变并且动画有效。 But somehow the Midi is not refreshed.但不知何故,Midi 没有刷新。 Midi has some issues, I do not know what. Midi有一些问题,我不知道是什么。

When the key changes CardRow is remounted and it destrows the animation.当键更改时 CardRow 被重新安装并取消动画。 Midi is also remounted and it works. Midi 也重新安装,它的工作原理。

My solution is that I use rowIndex as key.我的解决方案是我使用 rowIndex 作为键。 And I added RowNotation as key to the Midi.我添加了 RowNotation 作为 Midi 的关键。 Only Midi will be remounted.只有 Midi 会被重新安装。 And both animation and midi works.动画和midi都有效。

<Midi
  key={rowNotation}
  midiParams={{
    qpm: 80
  }}
  notation={rowNotation}
/>

https://codesandbox.io/s/transition-group-x-abc-midi-bug-hunt-so-rwjkk https://codesandbox.io/s/transition-group-x-abc-midi-bug-hunt-so-rwjkk

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

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