geolessel/react-phoenix

[question] How to use this lib with Redux?

abitdodgy opened this issue · 4 comments

Still getting to grips with React in general, so excuse the question if it sounds silly. In most examples I've seen, a Redux store is passed to the component when rendering it inside the JS file.

React.renderComponent(<MyComponent state={someStateStoreFromRedux} />, document.body);

Given that react-phoenix lets us render components from our eex templates, how would I go about using it with a Redux store?

scmx commented

@abitdodgy Here's what I do:

<%= react_component "myApp.MyComponent", something: "initial state" %>
function configureStore (initialState) {
  return createStore(...
}
class MyComponent extends React.Component {
  constructor (props) {
    super(props)
    const initialState = props
    this.store = configureStore(initialState)
  }

  render () {
    return (
      <Provider store={this.store}>
        ...
      </Provider>
    )
  }
}
window.myApp = {
  MyComponent
}
scmx commented

Hi @abitdodgy, did this solve your use case, can we close this?

@abitdodgy I'm going to go ahead and close this and consider it answered. If it doesn't clear up your question, feel free to respond.

@scmx that was indeed very helpful. Thank you for providing the example. @geolessel thanks!