Jared Moats
Jared Moats

Reputation: 55

React: Passing an object to state

I'm trying to make a replica of the Spotify Web Player by using the Spotify Web API. Right now, I'm trying to set the activePlaylist with an onClick event handler. However, when I pass my playlist object to the event handler method, the object is undefined. I've tried to fix this for a long time. Do you have any tips?

NOTE: It'll map the names to the page just fine, but the object becomes undefined when I pass it to the event handler method.

Here's my code:

import React, { Component } from 'react';
import '../css/playlists.css';

class Playlists extends Component {
  constructor(props) {
    super(props);

    this.state = {
      activePlaylist: null
    };
  }

  setActivePlaylist(playlist) {
    console.log('From setActivePlaylist');
    console.log(playlist.name); //this logs as undefined
  }

  render() {
    const {playlists} = this.props;

    return(
      <div className='playlists'>
        <h4 id='playlist-label'>Playlists</h4>
        {this.props.playlists
          ?
          (this.props.playlists.items.map((playlist, index)=>
            <a href="#" onClick={(playlist) => this.setActivePlaylist(playlist)}>
              <h4
              key={index}
              >
                {playlist.name}
              </h4>
            </a>
          ))
          :
          (<h4>Loading playlists...</h4>)
        }
      </div>
    );
  }
}

export default Playlists;

Upvotes: 4

Views: 1450

Answers (1)

anAgent
anAgent

Reputation: 2780

You will want to bind the context so you have access to the the class. Here's what it could look like the following snipped. I broke apart the logic simply for readability.

Also, you were trying to override the parameter of the event with the same name as the passed in variable - thus "playlist" was the event object, not the object you were expecting"

onClick={/* this be no good-->*/ (**playlist**) => this.setActivePlaylist(playlist)}

You can see a demo here: https://stackblitz.com/edit/react-passing-an-object-to-state?file=Playlists.js

import React, { Component } from 'react';

class Playlists extends Component {

  constructor(props) {
    super(props);

    this.state = {
      activePlaylist: null
    };

    // Need to bind the scoped context so we have access to "Playlists" component.
    this.renderLink = this.renderLink.bind(this);
  }

  setActivePlaylist(playlist) {
    console.log('From setActivePlaylist');
    console.log(playlist.name);
  }

  render() {
    const {items} = this.props.playlists

    return(
      <div className='playlists'>
        <h4 id='playlist-label'>Playlists</h4>
        {items
          ? items.map(this.renderLink)
          : <h4>Loading playlists...</h4>
        }
      </div>
    );
  }

  renderLink(playlist, index) {
    return (
      <a onClick={() => this.setActivePlaylist(playlist)}>
        <h4 key={index}>
            {playlist.name}
          </h4>
        </a>
    );
  }
}

export default Playlists;

Also make sure you bind setActivePlaylist in constructor or make it arrow function

Upvotes: 1

Related Questions