如何在 React 中隐藏/显示地图函数中的特定部分?
我有一个列出歌曲的页面。每首歌都有一些信息和视频(song.link)。我希望每首歌曲都有一个按钮来隐藏/显示每个视频。通过添加“假?”在“ReactPlayer”组件的前面和其末尾的“:null”我可以隐藏它。但我不知道如何通过带有变量的按钮来实现它。
import React,{Component,useState} from 'react';
import ReactPlayer from "react-player";
import './bootstrap.min.css';
export class Song extends Component{
constructor(props){
super(props);
this.state={songs:[]};
}
refreshList(){
fetch(process.env.REACT_APP_API+"song")
.then(response=>response.json())
.then(data=>{
this.setState({songs:data});
});
}
componentDidMount(){
this.refreshList();
}
componentDidUpdate(){
}
render(){
const {songs}=this.state;
return(
<div className="container col-20">
<tbody>
{songs.map(song=>(
<tr key={song.id}>
<div className="row turn">
<div className="jumbotron col-4">
<tr>{song.name}</tr>
<tr>{song.lyricsby}</tr>
<tr>{song.musicby}</tr>
<tr>{song.lyrics}</tr>
<tr>{song.albumName}</tr>
<button>Hide/Show Video</button>
</div>
<div className="jumbotron col-8">
<div className="mt-0 d-flex justify-content-right">
{
false?
<ReactPlayer
url={song.link}
/>:null}
</div>
</div>
</div>
</tr>
))}
</tbody>
</div>
)
}
}
I have a page that lists songs. Each song has some info and a video(song.link). I want to have a button for each song to hide/show each video. By adding "false?" at the front of "ReactPlayer" component and ":null" to end of it I can hide it. But I don't know how to make it through a button with a variable.
import React,{Component,useState} from 'react';
import ReactPlayer from "react-player";
import './bootstrap.min.css';
export class Song extends Component{
constructor(props){
super(props);
this.state={songs:[]};
}
refreshList(){
fetch(process.env.REACT_APP_API+"song")
.then(response=>response.json())
.then(data=>{
this.setState({songs:data});
});
}
componentDidMount(){
this.refreshList();
}
componentDidUpdate(){
}
render(){
const {songs}=this.state;
return(
<div className="container col-20">
<tbody>
{songs.map(song=>(
<tr key={song.id}>
<div className="row turn">
<div className="jumbotron col-4">
<tr>{song.name}</tr>
<tr>{song.lyricsby}</tr>
<tr>{song.musicby}</tr>
<tr>{song.lyrics}</tr>
<tr>{song.albumName}</tr>
<button>Hide/Show Video</button>
</div>
<div className="jumbotron col-8">
<div className="mt-0 d-flex justify-content-right">
{
false?
<ReactPlayer
url={song.link}
/>:null}
</div>
</div>
</div>
</tr>
))}
</tbody>
</div>
)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要绑定控制 ReactPlayer 渲染的“false”,并将 setState 添加到按钮 onClick 来切换布尔值
You will need to bind the "false" which controls the render of ReactPlayer and add setState to the button onClick which toggles the boolean value