简体   繁体   中英

mapDispatchToProps() in Connect(App) must return a plain object. Instead received [object Promise]

I am new to React and building a Spotify App with their API. I am using Redux Promise to resolve all promises. I can see data when I console it in my reducer of the data. But when I check my console it shows mapDispatchToProps() in Connect(App) must return a plain object. Instead received [object Promise]. I am thinking is it because I'm using Redux Promise vs thunk, but shouldn't it be able to resolve them as well?

Reducer

import { NEW_RELEASES }  from '../actions/types';

export default function(state = [] , action){
    console.log(action)
    switch(action.type){
        case NEW_RELEASES:
        return [ action.payload.data, ...state ];
    }
    return state
}

Store

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';

import App from './App';
import reducers from './reducers';
import ReduxPromise from 'redux-promise'; // Look at action creator for ReduxPromise use

const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);

ReactDOM.render(
  <Provider store={createStoreWithMiddleware(reducers)}>
    <App />
  </Provider>
  , document.querySelector('#root'));

Action Creator

export const getNewReleases = () => {
    console.log('ran')
        let request = axios.get("https://api.spotify.com/v1/browse/new-releases?country=SE", {
            headers: {
                'Authorization': 'Bearer ' + accessToken
            }
        })

        return{
            type: NEW_RELEASES,
            payload:  request
        }

App.Js

import React, { Component } from 'react';
import './App.css';
import Spotify from 'spotify-web-api-js';
import { getNewReleases }  from './actions'
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';



const spotifyWebApi = new Spotify();

class App extends Component {
  constructor(props) {
    super(props)
    const params = this.getHashParams();
    this.state = {
      welcome: "Welcome to SpotiDate",
      accessToken: params.access_token,
      loggedIn: params.access_Token ? true : false,
      nowPlaying: {
        name: 'Not Checked',
        images: ''
      }
    }
    if (params.access_token) {
      spotifyWebApi.setAccessToken(params.access_token);
    }
  }

  getHashParams() {
    var hashParams = {};
    var e, r = /([^&;=]+)=?([^&;]*)/g,
      q = window.location.hash.substring(1);
    while (e = r.exec(q)) {
      hashParams[e[1]] = decodeURIComponent(e[2]);
    }
    return hashParams;
  }

  componentWillMount() {
    spotifyWebApi.setAccessToken(this.state.accessToken)
    localStorage.setItem('accessToken', this.state.accessToken);
    const storedToken = localStorage.getItem('accessToken');
    getNewReleases(storedToken);
  }




  render() {
    return (
      <div className="App">
        <h3>{this.state.welcome}</h3>
        <div>
          <img src={this.state.nowPlaying.image} />
        </div>
        <button onClick={() => getNewReleases()}>Get New Releases</button>
        <div className="new-releases">
        </div>
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return{
    newReleases: state.newReleases
  }

}

const mapDispatchToProps = (dispatch) => { 
  return bindActionCreators(getNewReleases, dispatch);
}

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

the function bindActionCreators will take 1st argument as JSON. so it should be like below.

const mapDispatchToProps = dispatch => {
  return bindActionCreators(
    {
      getNewReleases: getNewReleases
    },
    dispatch
  );
};

try to use Object.assign({}, state.newReleases), or you can use the spread operator like assigning the code just like return {...state,state.newReleases}

since your object is unable to be mapped to the object state

For further understanding of this, please check this link git issue - 334

Try returning you actions in mapDispatchToProps like this:

const mapDispatchToProps = (dispatch) => ({
 getNewReleases: () => dispatch(getNewReleases())
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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