Reactjs 通过onChange将变量传递给类组件;选择>;标签

Reactjs 通过onChange将变量传递给类组件;选择>;标签,reactjs,Reactjs,如何将对象tracks[currentTrack]向下传递到类组件UserPlaylist。我还没有找到任何方法来做这件事 播放列表组件: render(){ return( <li><UserPlaylist onChange={(tracks)=tracks[currentTrack]}/></li> ); } export default Playlist render(){ 返回( ); } 导出默认播放列表 Us

如何将对象tracks[currentTrack]向下传递到类组件UserPlaylist。我还没有找到任何方法来做这件事

播放列表组件:

render(){
    return(
        <li><UserPlaylist onChange={(tracks)=tracks[currentTrack]}/></li>
    );
}
export default Playlist
render(){
返回(
  • ); } 导出默认播放列表
    UserPlaylist组件(不含javascript代码):

    render(){
    返回(
    添加到播放列表
    {
    this.state.users\u playlists.map((播放列表,索引)=>(
    {playlist.name}
    ))
    }
    );
    }
    }
    导出默认用户播放列表;
    
    如果您想在组件之间来回通信,我假设您使用的是基于类的组件,那么这就是如何实现的

    在播放列表组件中定义一个函数,您可以命名任何内容

    onChangeTracks=(e)=>{
          //You will get the value like this
          console.log(e.target.value)          
    
          //You can set your tracks here
          this.setState({
              currentTrack: tracks[currentTrack]
          })
    }
    
    然后,您需要将此函数作为道具传递给playlist组件中的UserPlayList,如下所示

     onChangeTracks=(e)=>{
           //You will get the value like this
          console.log(e.target.value)          
    
          //You can set your tracks here
          this.setState({
              currentTrack: tracks[currentTrack]
          })
    }
    
    render(){
          return(
                <li><UserPlaylist tracks={this.state.currentTrack} onChange={this.onChangeTracks}/></li>
          )
    }
    export default Playlist
    
    render(){
       //here you will get tracks data
       console.log("Props data", this.props.tracks);
    
       <select onChange={this.props.onChange}> 
       </select> 
    }
    
    onChangeTracks=(e)=>{
    //你会得到这样的值
    console.log(例如target.value)
    //你可以在这里设定你的轨迹
    这是我的国家({
    currentTrack:tracks[currentTrack]
    })
    }
    render(){
    返回(
    
  • ) } 导出默认播放列表
    在UserPlaylist组件的另一端,调用这个道具函数,如下所示

     onChangeTracks=(e)=>{
           //You will get the value like this
          console.log(e.target.value)          
    
          //You can set your tracks here
          this.setState({
              currentTrack: tracks[currentTrack]
          })
    }
    
    render(){
          return(
                <li><UserPlaylist tracks={this.state.currentTrack} onChange={this.onChangeTracks}/></li>
          )
    }
    export default Playlist
    
    render(){
       //here you will get tracks data
       console.log("Props data", this.props.tracks);
    
       <select onChange={this.props.onChange}> 
       </select> 
    }
    
    render(){
    //在这里您将获得跟踪数据
    log(“Props数据”,this.Props.tracks);
    }
    
    如果您想在组件之间来回通信,我假设您使用的是基于类的组件,那么这就是如何实现的

    在播放列表组件中定义一个函数,您可以命名任何内容

    onChangeTracks=(e)=>{
          //You will get the value like this
          console.log(e.target.value)          
    
          //You can set your tracks here
          this.setState({
              currentTrack: tracks[currentTrack]
          })
    }
    
    然后,您需要将此函数作为道具传递给playlist组件中的UserPlayList,如下所示

     onChangeTracks=(e)=>{
           //You will get the value like this
          console.log(e.target.value)          
    
          //You can set your tracks here
          this.setState({
              currentTrack: tracks[currentTrack]
          })
    }
    
    render(){
          return(
                <li><UserPlaylist tracks={this.state.currentTrack} onChange={this.onChangeTracks}/></li>
          )
    }
    export default Playlist
    
    render(){
       //here you will get tracks data
       console.log("Props data", this.props.tracks);
    
       <select onChange={this.props.onChange}> 
       </select> 
    }
    
    onChangeTracks=(e)=>{
    //你会得到这样的值
    console.log(例如target.value)
    //你可以在这里设定你的轨迹
    这是我的国家({
    currentTrack:tracks[currentTrack]
    })
    }
    render(){
    返回(
    
  • ) } 导出默认播放列表
    在UserPlaylist组件的另一端,调用这个道具函数,如下所示

     onChangeTracks=(e)=>{
           //You will get the value like this
          console.log(e.target.value)          
    
          //You can set your tracks here
          this.setState({
              currentTrack: tracks[currentTrack]
          })
    }
    
    render(){
          return(
                <li><UserPlaylist tracks={this.state.currentTrack} onChange={this.onChangeTracks}/></li>
          )
    }
    export default Playlist
    
    render(){
       //here you will get tracks data
       console.log("Props data", this.props.tracks);
    
       <select onChange={this.props.onChange}> 
       </select> 
    }
    
    render(){
    //在这里您将获得跟踪数据
    log(“Props数据”,this.Props.tracks);
    }
    
    能否在问题中添加更多代码?看起来您可能希望通过
    道具将其传递给播放列表组件,并将曲目存储在播放列表组件的
    状态
    ,但仅使用这两个渲染功能很难判断。您有
    此.state
    渲染
    功能,因此,我认为您处理的是类组件,而不是功能组件。您可以使用
    this.props
    访问类组件的道具。如果你想传递对象,你不需要使用
    onChange
    你可以放置
    track={tracks[currentTrack]}
    并从
    this.props.track
    访问它,你是在尝试访问播放列表组件中选择更改时用户播放列表组件中的选择值吗?你到底想传递什么?它看起来像
    onChange
    ,但这表示一个函数,但你说的是一个对象?你能在你的问题中添加更多的代码吗?看起来您可能希望通过
    道具将其传递给播放列表组件,并将曲目存储在播放列表组件的
    状态
    ,但仅使用这两个渲染功能很难判断。您有
    此.state
    渲染
    功能,因此,我认为您处理的是类组件,而不是功能组件。您可以使用
    this.props
    访问类组件的道具。如果你想传递对象,你不需要使用
    onChange
    你可以放置
    track={tracks[currentTrack]}
    并从
    this.props.track
    访问它,你是在尝试访问播放列表组件中选择更改时用户播放列表组件中的选择值吗?你到底想传递什么?它看起来像
    onChange
    ,但这表示一个函数,但你说的是一个对象?谢谢,对不起,我指的是类组件,我是新手,所以我把它搞错了。但是这个解决方案可以满足我的需要。虽然我有一个小问题。在用户更改了选择标记中的选项后,我发出了一个警告,说“track added”,但是我如何能让选择标记在警告后立即返回到value=DEFAULT。不管怎样,我已经找到了我问题的答案,再次感谢Hanks,对不起,我是指类组件,我是新手,所以我把它弄糊涂了。但是这个解决方案可以满足我的需要。虽然我有一个小问题。在用户更改选择标记中的选项后,我会发出一个警告,说“track added”,但如何让选择标记在警告后立即返回到value=DEFAULT。不管怎样,我已经找到了问题的答案,再次感谢