Javascript Connect(App)中的mapDispatchToProps()必须返回普通对象。相反,收到[反对承诺]
我是一个新的反应和建立一个Spotify应用程序与他们的API。我正在使用Redux承诺来解决所有承诺。当我在数据缩减器中对数据进行控制台时,我可以看到数据。但当我检查控制台时,它显示Connect(App)中的mapDispatchToProps()必须返回一个普通对象。相反,我们收到了[反对承诺]。我在想是不是因为我使用了Redux Promise vs thunk,但它不应该也能解决它们吗 减速器Javascript Connect(App)中的mapDispatchToProps()必须返回普通对象。相反,收到[反对承诺],javascript,reactjs,redux,react-redux-form,Javascript,Reactjs,Redux,React Redux Form,我是一个新的反应和建立一个Spotify应用程序与他们的API。我正在使用Redux承诺来解决所有承诺。当我在数据缩减器中对数据进行控制台时,我可以看到数据。但当我检查控制台时,它显示Connect(App)中的mapDispatchToProps()必须返回一个普通对象。相反,我们收到了[反对承诺]。我在想是不是因为我使用了Redux Promise vs thunk,但它不应该也能解决它们吗 减速器 import { NEW_RELEASES } from '../actions/type
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
}
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
}
商店
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'));
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);
import React,{Component}来自'React';
导入“/App.css”;
从“Spotify web api js”导入Spotify;
从“/actions”导入{getNewReleases}
从“redux”导入{bindActionCreators};
从'react redux'导入{connect};
const spotifyWebApi=new Spotify();
类应用程序扩展组件{
建造师(道具){
超级(道具)
const params=this.getHashParams();
此.state={
欢迎:“欢迎来到SpotiDate”,
accessToken:params.access_令牌,
loggedIn:params.access_令牌?true:false,
正在播放:{
名称:'未选中',
图像:“”
}
}
if(参数访问令牌){
spotifyWebApi.setAccessToken(参数访问令牌);
}
}
getHashParams(){
var hashParams={};
变量e,r=/([^&;=]+)=?([^&;]*)/g,
q=window.location.hash.substring(1);
while(e=r.exec(q)){
hashParams[e[1]]=decodeURIComponent(e[2]);
}
返回hashParams;
}
组件willmount(){
spotifyWebApi.setAccessToken(this.state.accessToken)
localStorage.setItem('accessToken',this.state.accessToken);
const storedToken=localStorage.getItem('accessToken');
getNewReleases(storedToken);
}
render(){
返回(
{本州,欢迎}
getNewReleases()}>获取新版本
);
}
}
常量mapStateToProps=(状态)=>{
返回{
newReleases:state.newReleases
}
}
const mapDispatchToProps=(调度)=>{
返回bindActionCreators(getNewReleases,dispatch);
}
导出默认连接(mapStateToProps、mapDispatchToProps)(应用程序);
尝试使用Object.assign({},state.newReleases),或者您可以使用spread操作符,就像分配代码一样,返回{…state,state.newReleases}
因为您的对象无法映射到对象状态
为了进一步了解这一点,请检查此链接尝试使用Object.assign({},state.newReleases),或者您可以使用spread运算符,就像分配代码一样,返回{…state,state.newReleases}
因为您的对象无法映射到对象状态
要进一步了解这一点,请查看此链接尝试在
mapDispatchToProps
中返回您的操作,如下所示:
const mapDispatchToProps = (dispatch) => ({
getNewReleases: () => dispatch(getNewReleases())
})
尝试在
mapDispatchToProps
中返回操作,如下所示:
const mapDispatchToProps = (dispatch) => ({
getNewReleases: () => dispatch(getNewReleases())
})
函数
bindActionCreators
将第一个参数作为JSON。所以应该像下面这样
const mapDispatchToProps = dispatch => {
return bindActionCreators(
{
getNewReleases: getNewReleases
},
dispatch
);
};
函数
bindActionCreators
将第一个参数作为JSON。所以应该像下面这样
const mapDispatchToProps = dispatch => {
return bindActionCreators(
{
getNewReleases: getNewReleases
},
dispatch
);
};
只是一个旁注:不要使用
mapDispathToProps
,只需将您的操作传递给connect()
函数,如下所示:导出默认连接(MapStateTrops,{getNewReleases})(应用程序)代码>谢谢,但我想知道您将如何调度您的操作?这些操作将在this.props
中提供。因此,在您的示例代码中,它应该是这样的:this.props.getNewReleases()
另请参见:只是一个旁注:不要使用mapDispathToProps
,只需在您的操作中传递给connect()
函数,如下所示:导出默认连接(MapStateTops,{getNewReleases})(应用程序)代码>谢谢,但我想知道您将如何调度您的操作?这些操作将在this.props
中提供。因此,在您的示例代码中,应该是这样的:this.props.getNewReleases()
另请参见:我已经尝试了对象分配,但它仍然返回承诺。我在想,如果我正确地解决了它,我已经尝试了对象分配,但它仍然返回承诺。我在想我是否正确地解决了它