forked from mirrorjs/mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
45 lines (33 loc) · 1.07 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { options } from './defaults'
import { models } from './model'
import { store, createStore, replaceReducer } from './store'
let started = false
let Root
export default function render(component, container, callback) {
const { initialState, middlewares, reducers } = options
if (started) {
// If app has rendered, do `store.replaceReducer` to update store.
replaceReducer(store, models, reducers)
// Call `render` without arguments means *re-render*. Since store has updated,
// `component` will automatically be updated, so no need to `ReactDOM.render` again.
if (arguments.length === 0) {
return Root
}
} else {
createStore(models, reducers, initialState, middlewares)
}
// Use named function get a proper displayName
Root = function Root() {
return (
<Provider store={store}>
{component}
</Provider>
)
}
started = true
global.document && ReactDOM.render(<Root/>, container, callback)
return Root
}