0

Делаю приложение на React JS. Непонятная проблема с инициализацией начального состояния. В консоли при выполнении команды npm start выдается такое сообщение

 bundle.js:23330 Unexpected keys "date", "currencies" found 
 inpreloadedState argument passed to createStore. Expected to find one of 
 the known reducer keys instead: "currencyReducer". Unexpected keys will be  
  ignored.

actions.js:

   export const refreshDate = (date, currencies)  => ({
     type: 'REFRESH_DATE', date, currencies
 });

reducer.js:

 export const currencyReducer = (state, action) => {
   switch(action.type) {
      case 'REFRESH_DATE':
      let new_state = Object.assign({}, { date : action.date, currencies :       action.currencies} );
      console.log(new_state);
      return new_state;
       default:
         return state || { date : new Date(), currencies : [] };
       }
     };

фрагмент app.js:

  const iniState = { date : new Date, currencies : [] };  
  export const store = createStore(combineReducers(reducers), iniState,  
    applyMiddleware(refreshDate));

Что я делаю не так?

4
  • А как вы подключаете reducers в app.js? 20 сен 2017 в 9:01
  • Вот так: import * as reducers from './reducers';
    – Andrew
    20 сен 2017 в 10:00
  • Ок, а что тогда из себя представляет ./reducers? 20 сен 2017 в 10:10
  • export const currencyReducer = (state, action) => { switch(action.type) { case 'REFRESH_DATE': let new_state = Object.assign({}, { date : action.date, currencies : action.currencies} ); console.log(new_state); return new_state; default: return state || { date : new Date(), currencies : [] }; } };
    – Andrew
    20 сен 2017 в 10:18

1 ответ 1

1

Аргумент preloadedState функции createStore (iniState) должен представлять из себя будущий стейт, который у Вас имеет такую структуру:

{
    currencyReducer: {
        date: new Date(), 
        currencies: []
    }
}

А Вы пытаетесь подсунуть ему

{ date : new Date(), currencies : [] }

На что явно указывает эта запись:

Unexpected keys "date", "currencies" found in preloadedState argument passed to createStore

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.