简体   繁体   English

将道具传递到React容器组件

[英]Pass props to React container component

I'm new to React. 我是React的新手。 I'm displaying a list of songs and I want to allow the user to add songs to their favourites. 我正在显示歌曲列表,我希望允许用户将歌曲添加到他们的收藏夹中。 I'm using Redux to store the favourited songs. 我正在使用Redux来存储收藏的歌曲。 My PlayList component looks like: 我的播放列表组件如下所示:

import AddSong from '../containers/AddSong'

class Playlist extends Component {

    render(){
        return (
          <div>
            <h1>Playlists</h1>
            <ul className="container">
              {this.state.items.map(item =>
                <li key={item.track.id}>
                    {item.track.name} by {item.track.artists[0].name}
                    <img src={item.track.album.images[0].url} height="150" width="150" />

                    <AddSong title={item.track.name} />

                </li>
              )}
            </ul>
          </div>
        );

    }
    ...
}

So I passing the song name to AddSong with <AddSong title={item.track.name} /> 所以我用<AddSong title={item.track.name} />将歌曲名称传递给AddSong

And AddSong looks like: 和AddSong看起来像:

import React from 'react'
import { connect } from 'react-redux'
import { addSong } from '../actions'

let AddSong = ({ dispatch }) => {
  let input

  console.log('this is ', this);

  return (
    <div>
      <form
        onSubmit={e => {
          e.preventDefault()
          // GET SONG FROM PROPS AND DISPATCH
          //dispatch(addSong(input.value))

        }}
      >
        <input
          ref={node => {
            input = node
          }}
        />
        <button type="submit">
          Add Song
        </button>
      </form>
    </div>
  )
}
AddSong = connect()(AddSong)

export default AddSong

However, this is an object with the property: 但是, this是一个具有以下属性的对象:

{
   a: Connect(props, context)
}

How do I get the song title in AddSong? 如何在AddSong中获得歌曲标题?

EDIT 编辑

So this is what I have now, Im passing the song title to AddSong here: 所以这就是我现在所拥有的,我在这里将歌曲标题传递给AddSong:

<AddSong song={item.track.name} title={item.track.name} />

I'm passing the song title in as song and title to show what happens. 我将歌曲标题作为歌曲和标题传递,以显示发生了什么。

In AddSong, I have: 在AddSong中,我有:

const mapStateToProps = (state) => {

    const {song:song} = state; // or whatever the reducer called
    return {song};
};

const mapDispatchToProps = (dispatch) => ({

    addSong: (value) => dispatch(addSong(value)),
});

export default connect(mapStateToProps, mapDispatchToProps)(AddSong);

And at the top of AddSong I'm doing: 在AddSong的顶部,我正在做:

let AddSong = ({ dispatch, ...props }) => {
  let input

  console.log('props is ', props);

The console outputs: 控制台输出:

props is  Object {song: undefined, title: "Young Blood"}

I've changed the button to: 我将按钮更改为:

<button onClick={(value)=>props.addSong(value)}>
  Add Song
</button>

When I click, this gives the error: 当我单击时,出现错误:

Failed prop type: Invalid prop `songs[0].text` of type `object` supplied to `SongList`, expected `string

Try to use this function 尝试使用此功能

const mapStateToProps = function(store) {
  return {
    data: store.data
  };
  }

AddSong = connect(mapStateToProps)(AddSong)

I assume you have the reducer and you want to access the state via props, if this the case you can mapStateToProps eg 我假设您有reducer,并且您想通过props访问状态,如果是这种情况,则可以mapStateToProps例如

 const mapStateToProps = (state) => { const {song:song} = state; // or whatever the reducer called return {song}; }; const mapDispatchToProps = (dispatch) => ({ addSong: (value) => dispatch(addSong(value)), }); export default connect(mapStateToProps, mapDispatchToProps)(AddSong); 

then you can just write this.props.song . 然后您可以编写this.props.song

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

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