我无法在Redux存储中保存数组吗?

时间:2020-03-26 00:26:33

标签: javascript reactjs react-native redux react-redux

在主屏幕中,我从API获取歌曲并将其保存为组件状态。现在,在渲染歌曲之后,当我按任意一个歌曲时,我将调度一个动作以将所有歌曲保存为 redux状态并调度其他操作以显示最差玩家“对/错” 但是我遇到了此错误

错误:动作必须是普通对象。使用自定义中间件进行异步 动作

我不知道为什么会出现,这只是我要保存的数组!

主屏幕

 _renderItem = ({item, index}) => {
    const {url} = this.state;
    return (
      <TouchableNativeFeed
        key={item.id}
        onPress={() => {
          console.log(
            'index-recent_tunes',
            index,
            this.state.recent_tunes,
          ); // I can see the output "array of songs" + index
          this.props.saveSongs(this.state.recent_tunes, index);
          this.props.isPlaying(true);
        }}
    .....
}


const mapDispatchToProps = dispatch => {
  return {
    isPlaying: _isPlaying => {
      dispatch(isPlayingAction(_isPlaying));
    },
    saveSongs: (songs, index) => {
      dispatch(songsInPlayerAction(songs, index));
    },
  };
};
export default connect(null, mapDispatchToProps)(Home);

Redux

减速器

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

let initialState = {
  allSongs: [],
  currentIndex: 0,
};

const songsInPlayerReducer = (state = initialState, action) => {
  switch (action.type) {
    case SONGS_IN_PLAYER:
      return {
        ...state,
        allSongs: action.songs,
        currentIndex: action.index,
      };
    default:
      return state;
  }
};

export default songsInPlayerReducer;

操作

import {SONGS_IN_PLAYER} from './types';

export const saveSongsPlayer = (songs, currentIndex) => {
  return {
    type: SONGS_IN_PLAYER,
    songs,
    currentIndex,
  };
};

商店

const persistConfig = {
  key: 'root',
  storage: AsyncStorage,
};

const rootReducer = combineReducers({
   ....
  songsInPlayer: songsInPlayerReducer,
  ....
});

const persistedReducer = persistReducer(persistConfig, rootReducer);


export const store = createStore(
  persistedReducer,
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
);
export const persistor = persistStore(store);

0 个答案:

没有答案