简体   繁体   English

Connect(App)中的mapDispatchToProps()必须返回一个普通对象。 而是收到了[object Promise]

[英]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. 我是React的新手,并使用其API构建Spotify应用。 I am using Redux Promise to resolve all promises. 我正在使用Redux Promise来兑现所有承诺。 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. 但是,当我检查控制台时,它显示Connect(App)中的mapDispatchToProps()必须返回一个普通对象。 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? 我在想这是因为我正在使用Redux Promise vs thunk,但它是否也能够解决它们?

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 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. 函数bindActionCreators将第一个参数作为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} 尝试使用Object.assign({},state.newReleases),也可以像使用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 要对此进一步了解,请检查此链接git问题-334

Try returning you actions in mapDispatchToProps like this: 尝试像这样在mapDispatchToProps返回操作:

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

暂无
暂无

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

相关问题 应用程序有效,但会产生错误:Connect(App)中的mapDispatchToProps()必须返回一个普通对象。 而是收到未定义 - Aplication works but generates an error: mapDispatchToProps() in Connect(App) must return a plain object. Instead received undefined React-Redux容器在Connect(ModalRoot)中抛出“ mapStateToProps()”时必须返回一个普通对象。 而是收到未定义的信息。” - React-Redux container throws “mapStateToProps() in Connect(ModalRoot) must return a plain object. Instead received undefined.” 动作必须是普通对象。 使用自定义中间件 - Action must be plain object. Use custom middleware 角度js-返回promise代替数据对象 - angular js - Return promise instead data object AngularJS服务返回一个对象而不是一个承诺 - AngularJS Service to Return an Object Instead of a Promise Firestore 返回 True 或 False 而不是 Promise Object - Firestore Return True or False instead of the Promise Object 如何让 axios 返回 object 而不是 promise - how to get axios to return the object instead of promise TypeError:第一个参数必须是字符串、Buffer、ArrayBuffer、Array 或类似数组的 Object 类型之一。 在 cryptoJS 中收到类型 object - TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object in cryptoJS TypeORM 返回纯 object - TypeORM return plain object 反应:行动必须是简单的 Object - React : Action Must Be Plain Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM