unirakun / k-redux-factory

Factory of Redux reducers and their associated actions and selectors.
MIT License
18 stars 4 forks source link

Error middleware with v6.0 #146

Closed vmenard closed 5 years ago

vmenard commented 5 years ago

Hello, I would like to migrate from 5.0.2 to 6.0 but I have this error when I define a middleware with helper mapPayload. This middleware worked with v5.0.2. Please help me :) because I would like to migrate for reduce my bundle.

Thanks.

Error :

Uncaught Error: Reducer "cities" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined

Example :


import { combineReducers } from 'redux'
import { keyValue, simple } from 'k-redux-factory'
import { mapPayload } from 'k-redux-factory/helpers'

const path = 'data.analyse'

const mapCity = city => ({
  text: city.label,
  ...city,
})
const mapCities = cities => cities.map(mapCity)

const middlewares = {
  pre: [
    mapPayload(/@@krf\/SET>DATA.ANALYSE>CITIES/)(mapCities),
  ],
}

export const cities = keyValue(middlewares)({ path, key: 'value', name: 'cities', prefix: path, defaultData: [] })
export const establishments = keyValue({ path, key: 'id', name: 'establishments', prefix: path })
export const firstActs = keyValue({ path, key: 'value', name: 'firstActs', prefix: path })
export const hasConsented = simple.bool({ path, name: 'hasConsented', defaultData: false, prefix: path })
export const hasDevisChirurgie = simple.bool({ path, name: 'hasDevisChirurgie', defaultData: false, prefix: path })
export const modeConnecte = simple.bool({ path, name: 'modeConnecte', defaultData: false, prefix: path })
export const result = simple.object({ path, name: 'result', prefix: path })
export const secondActs = keyValue({ path, key: 'value', name: 'secondActs', prefix: path })

export default combineReducers({
  cities,
  establishments,
  firstActs,
  hasConsented,
  hasDevisChirurgie,
  modeConnecte,
  result,
  secondActs,
})
fabienjuif commented 5 years ago

I try a second attempt to reproduce with the built version.

By now my first attempt is a failure reproducing your issue:

Code

const path = 'data.analyse'

const mapCity = city => ({ text: city.label, ...city, }) const mapCities = cities => cities.map(mapCity)

const middlewares = { pre: [ mapPayload(/@@krf\/SET>DATA.ANALYSE>CITIES/)(mapCities), ], }

export const cities = keyValue(middlewares)({ path, key: 'value', name: 'cities', prefix: path, defaultData: [] }) export const establishments = keyValue({ path, key: 'id', name: 'establishments', prefix: path }) export const firstActs = keyValue({ path, key: 'value', name: 'firstActs', prefix: path }) export const hasConsented = simple.bool({ path, name: 'hasConsented', defaultData: false, prefix: path }) export const hasDevisChirurgie = simple.bool({ path, name: 'hasDevisChirurgie', defaultData: false, prefix: path }) export const modeConnecte = simple.bool({ path, name: 'modeConnecte', defaultData: false, prefix: path }) export const result = simple.object({ path, name: 'result', prefix: path }) export const secondActs = keyValue({ path, key: 'value', name: 'secondActs', prefix: path })

const analyse = combineReducers({ cities, establishments, firstActs, hasConsented, hasDevisChirurgie, modeConnecte, result, secondActs, })

const store = createStore(combineReducers({ data: combineReducers({ analyse, }), }))

store.dispatch({ type: 'test' }) console.log(store.getState()) console.log(cities.getAsArray(store.getState()))

- result is : 

➜ k-redux-factory git:(master) ✗ node -r esm ./santeclair.js { data: { analyse: { cities: [Object], establishments: [Object], firstActs: [Object], hasConsented: false, hasDevisChirurgie: false, modeConnecte: false, result: {}, secondActs: [Object] } } } [] ➜ k-redux-factory git:(master) ✗

fabienjuif commented 5 years ago

Second attempt, reproducing the error :

➜  k-redux-factory git:(master) ✗ node -r esm ./santeclair.js
file:///Volumes/sensitive/work/k-redux-factory/node_modules/redux/lib/redux
.js:1
Error: Reducer "cities" returned undefined during initialization. If the st
ate passed to the reducer is undefined, you must explicitly return the init
ial state. The initial state may not be undefined. If you don't want to set
 a value for this reducer, you can use null instead of undefined.
    at file:///Volumes/sensitive/work/k-redux-factory/node_modules/redux/li
b/redux.js:366:13
    at Array.forEach (<anonymous>)
    at assertReducerShape (file:///Volumes/sensitive/work/k-redux-factory/n
ode_modules/redux/lib/redux.js:359:25)
    at combineReducers (file:///Volumes/sensitive/work/k-redux-factory/node
_modules/redux/lib/redux.js:422:5)
    at Object.<anonymous> (file:///Volumes/sensitive/work/k-redux-factory/s
anteclair.js:28:17)
    at Generator.next (<anonymous>)
➜  k-redux-factory git:(master) ✗

What I changed in code :

import { keyValue, simple } from './dist/index.es'
import { mapPayload } from './helpers/index.es'
fabienjuif commented 5 years ago

It works with built version of helper It breaks with built version of k-redux-factory

import { keyValue, simple } from './src'
// import { mapPayload } from './src/helpers'
// import { keyValue, simple } from './dist/index.es'
import { mapPayload } from './helpers/index.es'
fabienjuif commented 5 years ago

@vmenard I was a bug with microbundle, I upgraded it and it seems to work fine :)

https://github.com/alakarteio/k-redux-factory/pull/147

I am waiting for a review + I'll ping you when I will publish a new version.

vmenard commented 5 years ago

Great thanks @fabienjuif ;)

fabienjuif commented 5 years ago

You could maybeeee pay us a beer next time we see each other :)

fabienjuif commented 5 years ago

@vmenard this is published (v6.0.1)

vmenard commented 5 years ago

No problem for beer :)