Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React/Redux - Pass variable to action

I'm new to Redux and working on a project using the Spotify API. I'm sending API calls and retrieving data about the currently playing song.

I have a separate Redux action that is attempting to grab other albums from the currently playing artist. In my App.js I can access the id of the artist via const id = this.props.currentlyPlaying.id

I want to pass this variable from App.js to my album action. The action contains the API call and looks like this:

import SpotifyWebApi from 'spotify-web-api-js';
import {id} from '../App.js';
const spotifyApi = new SpotifyWebApi();

export function fetchAlbums() {
  return function(dispatch) {
    dispatch({ type: "FETCH_ALBUMS_BY_ARTIST"});

    //'43ZHCT0cAZBISjO8DG9PnE'
    spotifyApi.getArtistAlbums(id)
      .then((response) => {
        dispatch({ type: "FETCH_ALBUMS_BY_ARTIST_FULFILLED", payload: response})
      })
      .catch((err) => {
        dispatch({ type: "FETCH_ALBUMS_BY_ARTIST_REJECTED", payload: err})
      });

  }
}

I've tried to import the id variable but I get an error. What is the proper way to pass a variable from a component to a Redux action? I've also tried to access id directly in the action via this.props, that doesn't work either.

like image 469
Nick Kinlen Avatar asked Oct 18 '18 22:10

Nick Kinlen


People also ask

What is mapStateToProps and mapDispatchToProps?

The mapStateToProps and mapDispatchToProps deals with your Redux store's state and dispatch , respectively. state and dispatch will be supplied to your mapStateToProps or mapDispatchToProps functions as the first argument.

What is action payload in Redux?

Actions are the only source of information for the store as per Redux official documentation. It carries a payload of information from your application to store. As discussed earlier, actions are plain JavaScript object that must have a type attribute to indicate the type of action performed.

Can Redux action return value?

Redux actions do not return anything. They just trigger changes of the global state which you can then listen to. If you need to e.g. fetch data (to update the state with) inside an action you need some kind of async action which e.g. redux-thunk provides.


1 Answers

Don't try to import the id, pass it as a parameter when calling the fetchAlbums action.

Something like:

ACTION:

export const fetchAlbums = (id) => async dispatch => {
  dispatch({ type: "FETCH_ALBUMS_BY_ARTIST"});

  //'43ZHCT0cAZBISjO8DG9PnE'
  spotifyApi.getArtistAlbums(id)
    .then((response) => {
      dispatch({ type: "FETCH_ALBUMS_BY_ARTIST_FULFILLED", payload: response})
    })
    .catch((err) => {
      dispatch({ type: "FETCH_ALBUMS_BY_ARTIST_REJECTED", payload: err})
    });
}

USING ACTION:

This will bind a function to call the fetchAlbums function, allowing you to pass parameters instead of the click event.

<button onClick={() => fetchAlbums(id)}>Fetch Album</button>
like image 92
Mark Small Avatar answered Oct 01 '22 11:10

Mark Small