javascript - 从另一个组件更新一个组件的状态
问题描述
我想从另一个组件更新组件的图标。每当我单击播放列表音乐开始播放并且图标应该更改为暂停而不是播放但我不知道如何从另一个组件更新组件的状态。
PlayList 组件 -
播放列表和音乐都在这个组件中
class PlayList extends React.Component {
render() {
const playMusic = () => {
musics.forEach(e => e.pause());
musics[this.props.arr].play();
musics[this.props.arr].currentTime = 0;
nowPlaying = this.props.arr;
clickedOnMusic = 'clicked';
};
return (
<div>
<Card>
<CardActionArea onClick={playMusic} />
</Card>
</div>
)
}
BottomAppBar 组件 -
图标和一些播放音乐的功能在这里
class BottomAppBar extends React.Component {
state = {
displayPlay: 'none',
displayPause: '',
displayVolume: '',
displayMute: 'none'
};
render(){
return(
<IconButton onClick={handleChangePlay} style={{ color: 'white' }}>
<PauseCircleOutlineRoundedIcon
style={{ fontSize: 46, display: this.state.displayPlay }}
/>
<PlayCircleOutlineIcon
style={{ fontSize: 46, display: this.state.displayPause }}
/>
)
}
非常感谢您的阅读!
解决方案
将它们包装在一个容器中并在那里保持它们的状态。前任:
<Parent>
<PlayList/>
<BottomAppBar />
</Parent>
推荐阅读
- javascript - 为什么输入错误:错误:未捕获的类型错误:无法读取未定义的属性“indexOf”
- flutter - Flutter,如何使容器在一段时间后消失?
- javascript - 如何在其他标签上的标签上创建相同的鼠标悬停行为?
- ruby - 需要帮助来创建广度优先搜索功能
- jenkins - 嵌入式开发的 CI 策略 - 需要硬件或手动干预的测试
- python - SOCKET PROGRAMMING PYTHON - 将服务器代码放在在线服务器上
- java - 参考 JavaDoc 和系统文档
- javascript - 需要帮助从浏览器使用 MQTT 和 Ably
- reactjs - 使用 React Hooks 更改对象内的数组
- c - 如何在不使用帧的情况下通过以太网电缆发送原始位?