Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(root): dispatch APP_INIT and render <Root> only once
Browse files Browse the repository at this point in the history
feat(root): dispatch APP_INIT and render <Root> only once
  • Loading branch information
Metnew committed Feb 18, 2018
1 parent c358e0f commit 343e982
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/common/components/Root/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,31 @@ const Router = process.env.BROWSER
? require('react-router-redux').ConnectedRouter
: require('react-router').StaticRouter

export default class Root extends Component<Props> {
let initAlready = false
// react-async-bootstrapper renders <Root /> twice, because it's based on react-tree-walker
// react-tree-walker walks in React node tree and resolves promises.
// This behaviour allow apps to make server-side data fetching.
// But this approach has 2 drawbacks:
// Root rendered twice + APPLICATION_INIT dispatched twice
// `initAlready` ensures that `APPLICATION_INIT` was dispatched only once.
class Root extends Component<Props> {
static defaultProps = {
SSR: {}
}

componentWillMount () {
const {store, i18n} = this.props
store.dispatch({type: APPLICATION_INIT})
addLocaleData(i18n.localeData)
if (!initAlready) {
store.dispatch({type: APPLICATION_INIT})
addLocaleData(i18n.localeData)
}
initAlready = true
}

render () {
if (!initAlready) {
return null
}
const {SSR, store, history, i18n} = this.props
const routerProps = process.env.BROWSER
? {history}
Expand All @@ -59,3 +72,5 @@ export default class Root extends Component<Props> {
)
}
}

export default Root

0 comments on commit 343e982

Please sign in to comment.