Skip to content

Why the special entry point? #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
justin808 opened this issue Mar 25, 2017 · 4 comments
Closed

Why the special entry point? #1

justin808 opened this issue Mar 25, 2017 · 4 comments

Comments

@justin808
Copy link

justin808 commented Mar 25, 2017

Could you explain these two bullets a bit. This strategy needs to be a bit different with many entry points. Should React on Rails support this wrapping?

  • wrap elements in AppContainer from react-hot-loader
  • create a render function with reactDOM and use it for initial rendering and in module.hot.accept

https://github.com/retroalgic/react-on-rails-hot-minimal/blob/master/client/src/entry.js#L45

  if (module.hot) {
    module.hot.accept("components/Base", () => { renderApp(Base) })
    module.hot.accept("reducer", () => {
      appStore.replaceReducer(reducer)
    })
  }
@justin808 justin808 changed the title Why Why the special entry point? Mar 25, 2017
@mongjong59
Copy link
Owner

mongjong59 commented Mar 25, 2017

@justin808 I just made a change (move Provider and ConnectedRouter to Base)

The setup up should be similar with any entry point. I don't really think any magic from React on Rails side would make things simpler. I assume that way people will have to pass special options and arguments around even on Rails side while we are able to simply handle all things within the client code. So I think it's unnecessary React on Rails need to do any extra wrapping with this. Some documentation on this would be helpful enough.

@mongjong59
Copy link
Owner

The module.hot.accept magic is actually removed intentionally by maintainer of redux and react-hot-loader so it can be called in an appropriate place to avoid problems in some special cases (I don't know whether such case exists when using React on Rails, but I'd prefer no magic)
reduxjs/redux#667

@ezmiller
Copy link

ezmiller commented Sep 6, 2017

@retroalgic: I'm a bit curious about this as well. Looking at your entry.js, you have a stateless functional component that doesn't seem to return any JSX:

const App = (props, railsContext, domNodeId) => {
  const store = ReactOnRails.getStore("store")
  const renderApp = (Component) => {
    const element = (
      <AppContainer errorReporter={consoleErrorReporter}>
        <Component {...{ store, history }} />
      </AppContainer>
    )
    render(element, document.getElementById(domNodeId))
  }
  renderApp(Base)
  if (module.hot) {
    module.hot.accept(["reducer", "components/Base"], () => {
      store.replaceReducer(reducer)
      renderApp(Base)
    })
  }
}

Instead it calls ReactDom.render. I've never seen the render function called inside a component's render function. Can you explain how/why this works?

@mongjong59
Copy link
Owner

mongjong59 commented Sep 7, 2017

@ezmiller Starting from 3, React Hot Loader doesn't hide the HMR API any more, which is to say, you should at least use ReactDOM.render explicitly with if (module.hot) { module.hot.accept(...) } instead of just returning JSX (the old way).

As for the renderApp function, I wrote it to dry the code. The App here is a renderer function, with which I can reuse renderApp.

You can fork the rep and try to do it differently. Any suggestion is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants