I have main component which has 4 subcomponents and I wish to pass states props and functions between them. The thing is that in subcomponent List I wish to be able to get access only to interior of the list with the class ".title" Is it any way to jump between same classes in react as it was possible in jQuery? something like this next(), prev() and find()? I tried to find any way but I have failed so far.
class List extends React.Component {
constructor(props) {
super(props)
this.state = {
title: ""
}
}
render() {
return (
<div>
<ul className="List">
<li>
<ul className="song">
<li className="time">
3:16
</li>
<li className="title" onClick= {this.handleSong}> Title I want to pass
</li>
</ul>
</li>
<ul className="List">
<li>
<ul className="song">
<li className="time">
4:16
</li>
<li className="title" onClick= {this.handleSong}> next Title I want to pass
</li>
</ul>
</li>
</div>
and here is function where I can get access to one element but don't know how to switch to next one
handleSong = (event) => {
this.setState({
title: event.currentTarget.textContent,
})
event.preventDefault()
}
UPDATED:
I changed my code as you suggested and now it looks like this:
Inside main component:
class Widget extends React.Component {
constructor(props) {
super(props)
this.state = {
isClicked: true,
playerDisplayed: true,
title: "Still Don't Know",
songs: []
}
}
componentDidMount() {
const url = "http://localhost:3000/ListOfSongs"
fetch(url)
.then(resp => resp.json())
.then(data => {
this.setState({
songs: data
})
console.log(data);
});
}
return (
<div className="container">
<div className="wrapperList">
<HeaderList
handleReturn={this.handleReturn.bind(this)}/>
<ul className="songsList">
{this.state.songs.map(e =>
<SongsList id={e.id} title={e.title} time={e.time}
handleChooseSong={this.handleChooseSong.bind(this)}
playerDisplayed={this.state.playerDisplayed}/>
)}
</ul>
</div>
</div>
)
}
SongList component :
<li id={this.props.id}>
<ul className="song">
<li className="timeAndBand">
{this.props.time} || {this.props.band}
</li>
<li className="title" onClick={this.props.handleChooseSong}>
{this.props.title}
</li>
<li className="icons">
<svg aria-hidden="true" data-prefix="fas" data-icon="share-alt"className="shareBtn" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">
</svg>
<svg aria-hidden="true" data-prefix="fas" data-icon="heart"
className="heartBtn" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
</svg>
</li>
</ul>
</li>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…