MichalZalecki / connect-rxjs-to-react

Connect rxjs to React component in Redux style... but without dispatch and constants.
187 stars 27 forks source link

Selector error #4

Closed b2whats closed 6 years ago

b2whats commented 8 years ago
// observerNext: 1  Object {}
// observerNext: 2  Object {counter: Object}
// observerNext: 3  Object {counter: Object, otherCounter: Object}
export default connect(state$, state => (console.log(`observerNext: ${cnt++} `, state, ), {
  //counter: state.counter && state.counter.counter,
  counter: state.counter.counter, // will be error - Cannot read property 'counter' of undefined
...
}))(Counter);
b2whats commented 8 years ago

if change file createState.js on:

function createState(reducer$, initialState$ = Rx.Observable.of({})) {
  const publisher$ = initialState$
    .merge(reducer$)
    .scan((state, [scope, reducer]) => ({ ...state, [scope]: reducer(state[scope]) }))
    .publishReplay(1)

  publisher$.connect()

  return publisher$
}

there will be no error, the component will receive primary merge condition