【问题标题】:React/Redux Test Error: The initialState argument passed to createStore is of unexpected typeReact/Redux 测试错误:传递给 createStore 的 initialState 参数属于意外类型
【发布时间】:2018-01-09 17:59:55
【问题描述】:

尝试在我的 react/redux 套件中运行测试后,我收到以下错误。我已经确保我使用redux-immutable 作为文档在回购中的建议,但无济于事。

The initialState argument passed to createStore is of unexpected type. Expected argument to be an instance of Immutable.Collection or Immutable.Record with the following properties:

我正在使用Immutable.JS()

减速器文件:

import { combineReducers } from 'redux-immutable';
import dashboard from '../Dashboard';
import budget from '../Budget';
import { reducer as formReducer } from 'redux-form/immutable';
import { syncHistoryWithStore, routerReducer } from 'react-router-redux'

const rootReducer = combineReducers({
  form: formReducer,
  dashboard,
  budget
});

export default rootReducer;

存储文件

import {createStore, applyMiddleware, compose} from 'redux';
import thunk from 'redux-thunk';
import reducers from './reducers';

export function configureStore(){
  // Use redux dev tools if available, otherwise use default composition;
  const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

  const store = createStore(reducers, {}, composeEnhancers(
    applyMiddleware(thunk)
  ));

  return store;
}

尝试

我在store 文件中尝试了以下操作,因为我不知道错误是什么,但它仍然没有工作。

  const store = createStore(reducers, Immutable.collection({form: "", dashboard: "", budget: ""}), composeEnhancers(
    applyMiddleware(thunk)
  ));

我还尝试了以下方法:

https://github.com/gajus/redux-immutable

  const store = createStore(reducers, Immutable.Map(), composeEnhancers(
    applyMiddleware(thunk)
  ));

我尝试制作一个 Record 不可变对象

  const StateRecord = Immutable.Record({
    "form": "form",
    "dashboard": "dashboard",
    "budget": "budget"
  });

  const store = createStore(reducers, StateRecord, composeEnhancers(
    applyMiddleware(thunk)
  ));

【问题讨论】:

  • initialState 未在您提供的代码中的任何位置定义。为什么不尝试使用空对象? createStore(reducers, {}, ...)
  • @nbkhope 我忘了我之前添加了这个,看看我是否能找到解决办法。对于空对象,我仍然会遇到同样的错误。
  • @nbkhope 添加了一些我尝试过的新东西,但仍然没有

标签: reactjs redux react-redux immutable.js


【解决方案1】:

不幸的是,我应该提供我的测试运行程序,因为它是问题的根源。将新的提供者渲染到我的组件中..

测试运行器片段

<Provider store={createStore(reducers, Immutable.fromJS(state))}>
  <ComponentClass {...props} />
</Provider>

原来的样子

<Provider store={createStore(reducers, (state))}>
  <ComponentClass {...props} />
</Provider>

【讨论】:

  • 这里有同样的问题。究竟是什么解决方案?
猜你喜欢
  • 2017-02-24
  • 2019-09-05
  • 1970-01-01
  • 2019-10-06
  • 2018-02-07
  • 2016-06-25
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多