0

I want to make a simple application as a proof of concept for react components, and I was wondering if there was a way to implement recompose in such a way that it creates a store accesible from anywhere. This is what I have tried:

import App from './app-container'
import { withContext } from 'recompose'
import React, {
    Component,
    PropTypes,
} from 'react'

// Sets up application store.
const provide = store => withContext(
  { store: PropTypes.object },
  () => ({ store })
)

var appStore = {
  toast: null,
  test: "hi"
}

const AppWithContext = provide(appStore)(App)

export default AppWithContext
FluffySamurai
  • 558
  • 5
  • 9

1 Answers1

1

Your code just put the store into the context, which is indeed something similar to react-redux/Provider, but you also need to implement something like react-redux/connect, which is more complicated, not recompose good at.

Isaddo
  • 438
  • 4
  • 10
  • You still can implement react-redux/connect by combining recompose's getComtext, mapProps and pure. – wuct Jun 25 '17 at 03:29
  • @wuct I think the main task of `react-redux/connect` is after updating `store`, inform the connected components to re-render. Yeah, it is possible to implement by `recompose` but after the listeners of `store` got called, we need to trigger re-render, so we also need `withState` to achieve it. – Isaddo Jun 25 '17 at 04:31
  • I thought about it in depth, we also need `lifecycle` to control the store subscription, but I have seen some discussion about removing `withState` and `lifecycle` from `recompose`. – Isaddo Jun 25 '17 at 04:53
  • 2
    You're right. I forgot that we also need to listen to the store. IMO the best way to achieve it is use `mapPropsStream`. – wuct Jun 27 '17 at 09:50