af83/chouette-core

View on GitHub
app/packs/src/helpers/hooks/useStore.js

Summary

Maintainability
A
0 mins
Test Coverage
import { useState, useEffect } from 'react'
import { filter, map } from 'rxjs/operators'
import { isEqual } from 'lodash'

export default function useStore(
  store,
  mapStateToProps = state => state
) {
  const [state, setState] = useState(() => mapStateToProps(store.initialState))

  useEffect(() => {
    const sub = store.pipe(
      map(mapStateToProps),
      filter(newState => !isEqual(state, newState))
    ).subscribe(setState)

    // return () => sub.unsubsribe()
  }, [])

  return state
}