Eugene Podoliako Eugene Podoliako - 1 year ago 594
React JSX Question

Redux: Unexpected key found in preloadedState argument passed to createStore

Can you help me with exception

Unexpected key "userName" found in preloadedState argument passed to createStore. Expected to find one of the known reducer keys instead: "default". Unexpected keys will be ignored.

I discovered this Link but it doesn't help me. I don't undestand something, maybe this part from documentation: plain object with the same shape as the keys passed to it

Can you exlain me my mistake on my example?

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from 'react-redux';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import App from './containers/App.jsx';
import * as reducers from './reducers'
import types from './constants/actions';

const reducer = combineReducers(reducers);

const destination = document.querySelector("#container");

var store = createStore(reducer, {
userName : ''

<Provider store={store}>


store.subscribe( ()=> {
let s = store.getState()
console.log("STATE = " + s)
for (var k in s) {
if (s.hasOwnProperty(k)) {
console.log("k = " + k + "; value = " + s[k]);

type: types.LOAD_USER_NAME,
userName : "Oppps1"

my reducer:

import types from './../constants/actions'

export default function userNameReducer (state = {userName : 'N/A'}, action) {
console.log('inside reducer');
switch (action.type) {
case types.LOAD_USER_NAME:
console.log("action.userName = " + action.userName)
for (var k in state) {
if (state.hasOwnProperty(k)) {
console.log("k = " + k + "; value = " + state[k]);
return action.userName;
return state

result in console after execution:

enter image description here

Answer Source

TLDR: stop using combineReducers and pass your reducer to createStore directly. Use import reducer from './blabla' instead of import * from './blabla'.

Second argument of createStore, i.e. preloaded state must have the same object structure as your combined reducers. combineReducers takes an object, and applies each reducer that is provided in the object to the correspondant state property. Now you are exporting your reducer using export default, which is transpiled to something like module.exports.default = yourReducer. When you import the reducer, you get module.exports, which is equal to {default: yourReducer}. Your preloaded state doesn't have a default property thus redux complains. If you use import reducer from './blabla' you get module.exports.default instead which is equal to your reducer.

Please take your time and read how ES6 module system works from MDN.

Also read combineReducers docs from redux.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download