Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
rename createStore param initialState to preloadedState
  • Loading branch information
dliv committed May 26, 2016
commit 1dc04807b2e6a49ece909d9f1dfa57223c94e35d
4 changes: 2 additions & 2 deletions docs/Glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ See the complete [store API reference](api/Store.md#dispatch) for more details.
## Store creator

```js
type StoreCreator = (reducer: Reducer, initialState: ?State) => Store
type StoreCreator = (reducer: Reducer, preloadedState: ?State) => Store
```

A store creator is a function that creates a Redux store. Like with dispatching function, we must distinguish the base store creator, [`createStore(reducer, initialState)`](api/createStore.md) exported from the Redux package, from store creators that are returned from the store enhancers.
A store creator is a function that creates a Redux store. Like with dispatching function, we must distinguish the base store creator, [`createStore(reducer, preloadedState)`](api/createStore.md) exported from the Redux package, from store creators that are returned from the store enhancers.

## Store enhancer

Expand Down
2 changes: 1 addition & 1 deletion docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This section documents the complete Redux API. Keep in mind that Redux is only c

### Top-Level Exports

* [createStore(reducer, [initialState])](createStore.md)
* [createStore(reducer, [preloadedState])](createStore.md)
* [combineReducers(reducers)](combineReducers.md)
* [applyMiddleware(...middlewares)](applyMiddleware.md)
* [bindActionCreators(actionCreators, dispatch)](bindActionCreators.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/api/applyMiddleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export default connect(

const store = createStore(
reducer,
initialState,
preloadedState,
applyMiddleware(...middleware)
)
```
Expand Down
4 changes: 2 additions & 2 deletions docs/api/createStore.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `createStore(reducer, [initialState], [enhancer])`
# `createStore(reducer, [preloadedState], [enhancer])`

Creates a Redux [store](Store.md) that holds the complete state tree of your app.
There should only be a single store in your app.
Expand All @@ -7,7 +7,7 @@ There should only be a single store in your app.

1. `reducer` *(Function)*: A [reducing function](../Glossary.md#reducer) that returns the next [state tree](../Glossary.md#state), given the current state tree and an [action](../Glossary.md#action) to handle.

2. [`initialState`] *(any)*: The initial state. You may optionally specify it to hydrate the state from the server in universal apps, or to restore a previously serialized user session. If you produced `reducer` with [`combineReducers`](combineReducers.md), this must be a plain object with the same shape as the keys passed to it. Otherwise, you are free to pass anything that your `reducer` can understand.
2. [`preloadedState`] *(any)*: The initial state. You may optionally specify it to hydrate the state from the server in universal apps, or to restore a previously serialized user session. If you produced `reducer` with [`combineReducers`](combineReducers.md), this must be a plain object with the same shape as the keys passed to it. Otherwise, you are free to pass anything that your `reducer` can understand.

3. [`enhancer`] *(Function)*: The store enhancer. You may optionally specify it to enhance the store with third-party capabilities such as middleware, time travel, persistence, etc. The only store enhancer that ships with Redux is [`applyMiddleware()`](./applyMiddleware.md).

Expand Down
26 changes: 13 additions & 13 deletions docs/recipes/ServerRendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ app.use(handleRender)

// We are going to fill these out in the sections to follow
function handleRender(req, res) { /* ... */ }
function renderFullPage(html, initialState) { /* ... */ }
function renderFullPage(html, preloadedState) { /* ... */ }

app.listen(port)
```
Expand Down Expand Up @@ -83,23 +83,23 @@ function handleRender(req, res) {
)

// Grab the initial state from our Redux store
const initialState = store.getState()
const preloadedState = store.getState()

// Send the rendered page back to the client
res.send(renderFullPage(html, initialState))
res.send(renderFullPage(html, preloadedState))
}
```

### Inject Initial Component HTML and State

The final step on the server side is to inject our initial component HTML and initial state into a template to be rendered on the client side. To pass along the state, we add a `<script>` tag that will attach `initialState` to `window.__INITIAL_STATE__`.
The final step on the server side is to inject our initial component HTML and initial state into a template to be rendered on the client side. To pass along the state, we add a `<script>` tag that will attach `preloadedState` to `window.__INITIAL_STATE__`.

The `initialState` will then be available on the client side by accessing `window.__INITIAL_STATE__`.
The `preloadedState` will then be available on the client side by accessing `window.__INITIAL_STATE__`.

We also include our bundle file for the client-side application via a script tag. This is whatever output your bundling tool provides for your client entry point. It may be a static file or a URL to a hot reloading development server.

```js
function renderFullPage(html, initialState) {
function renderFullPage(html, preloadedState) {
return `
<!doctype html>
<html>
Expand All @@ -109,7 +109,7 @@ function renderFullPage(html, initialState) {
<body>
<div id="root">${html}</div>
<script>
window.__INITIAL_STATE__ = ${JSON.stringify(initialState)}
window.__INITIAL_STATE__ = ${JSON.stringify(preloadedState)}
</script>
<script src="/static/bundle.js"></script>
</body>
Expand Down Expand Up @@ -139,10 +139,10 @@ import App from './containers/App'
import counterApp from './reducers'

// Grab the state from a global injected into server-generated HTML
const initialState = window.__INITIAL_STATE__
const preloadedState = window.__INITIAL_STATE__

// Create Redux store with initial state
const store = createStore(counterApp, initialState)
const store = createStore(counterApp, preloadedState)

render(
<Provider store={store}>
Expand Down Expand Up @@ -182,10 +182,10 @@ function handleRender(req, res) {
const counter = parseInt(params.counter, 10) || 0

// Compile an initial state
let initialState = { counter }
let preloadedState = { counter }

// Create a new Redux store instance
const store = createStore(counterApp, initialState)
const store = createStore(counterApp, preloadedState)

// Render the component to a string
const html = renderToString(
Expand Down Expand Up @@ -245,10 +245,10 @@ function handleRender(req, res) {
const counter = parseInt(params.counter, 10) || apiResult || 0

// Compile an initial state
let initialState = { counter }
let preloadedState = { counter }

// Create a new Redux store instance
const store = createStore(counterApp, initialState)
const store = createStore(counterApp, preloadedState)

// Render the component to a string
const html = renderToString(
Expand Down
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ export interface Store<S> {
/**
* A store creator is a function that creates a Redux store. Like with
* dispatching function, we must distinguish the base store creator,
* `createStore(reducer, initialState)` exported from the Redux package, from
* `createStore(reducer, preloadedState)` exported from the Redux package, from
* store creators that are returned from the store enhancers.
*
* @template S State object type.
*/
export interface StoreCreator {
<S>(reducer: Reducer<S>, enhancer?: StoreEnhancer<S>): Store<S>;
<S>(reducer: Reducer<S>, initialState: S, enhancer?: StoreEnhancer<S>): Store<S>;
<S>(reducer: Reducer<S>, preloadedState: S, enhancer?: StoreEnhancer<S>): Store<S>;
}

/**
Expand All @@ -218,7 +218,7 @@ export interface StoreCreator {
*/
export type StoreEnhancer<S> = (next: StoreEnhancerStoreCreator<S>) => StoreEnhancerStoreCreator<S>;
export type GenericStoreEnhancer = <S>(next: StoreEnhancerStoreCreator<S>) => StoreEnhancerStoreCreator<S>;
export type StoreEnhancerStoreCreator<S> = (reducer: Reducer<S>, initialState: S) => Store<S>;
export type StoreEnhancerStoreCreator<S> = (reducer: Reducer<S>, preloadedState: S) => Store<S>;

/**
* Creates a Redux store that holds the state tree.
Expand All @@ -234,7 +234,7 @@ export type StoreEnhancerStoreCreator<S> = (reducer: Reducer<S>, initialState: S
* @param reducer A function that returns the next state tree, given the
* current state tree and the action to handle.
*
* @param [initialState] The initial state. You may optionally specify it to
* @param [preloadedState] The initial state. You may optionally specify it to
* hydrate the state from the server in universal apps, or to restore a
* previously serialized user session. If you use `combineReducers` to
* produce the root reducer function, this must be an object with the same
Expand Down
4 changes: 2 additions & 2 deletions src/applyMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import compose from './compose'
* @returns {Function} A store enhancer applying the middleware.
*/
export default function applyMiddleware(...middlewares) {
return (createStore) => (reducer, initialState, enhancer) => {
var store = createStore(reducer, initialState, enhancer)
return (createStore) => (reducer, preloadedState, enhancer) => {
var store = createStore(reducer, preloadedState, enhancer)
var dispatch = store.dispatch
var chain = []

Expand Down
2 changes: 1 addition & 1 deletion src/combineReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function getUndefinedStateErrorMessage(key, action) {
function getUnexpectedStateShapeWarningMessage(inputState, reducers, action) {
var reducerKeys = Object.keys(reducers)
var argumentName = action && action.type === ActionTypes.INIT ?
'initialState argument passed to createStore' :
'preloadedState argument passed to createStore' :
'previous state received by the reducer'

if (reducerKeys.length === 0) {
Expand Down
14 changes: 7 additions & 7 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export var ActionTypes = {
* @param {Function} reducer A function that returns the next state tree, given
* the current state tree and the action to handle.
*
* @param {any} [initialState] The initial state. You may optionally specify it
* @param {any} [preloadedState] The initial state. You may optionally specify it
* to hydrate the state from the server in universal apps, or to restore a
* previously serialized user session.
* If you use `combineReducers` to produce the root reducer function, this must be
Expand All @@ -36,26 +36,26 @@ export var ActionTypes = {
* @returns {Store} A Redux store that lets you read the state, dispatch actions
* and subscribe to changes.
*/
export default function createStore(reducer, initialState, enhancer) {
if (typeof initialState === 'function' && typeof enhancer === 'undefined') {
enhancer = initialState
initialState = undefined
export default function createStore(reducer, preloadedState, enhancer) {
if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {
enhancer = preloadedState
preloadedState = undefined
}

if (typeof enhancer !== 'undefined') {
if (typeof enhancer !== 'function') {
throw new Error('Expected the enhancer to be a function.')
}

return enhancer(createStore)(reducer, initialState)
return enhancer(createStore)(reducer, preloadedState)
}

if (typeof reducer !== 'function') {
throw new Error('Expected the reducer to be a function.')
}

var currentReducer = reducer
var currentState = initialState
var currentState = preloadedState
var currentListeners = []
var nextListeners = currentListeners
var isDispatching = false
Expand Down
4 changes: 2 additions & 2 deletions test/typescript/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const reducer: Reducer<State> = (state: State, action: Action): State => {

const store: Store<State> = createStore<State>(reducer);

const storeWithInitialState: Store<State> = createStore(reducer, {
const storeWithPreloadedState: Store<State> = createStore(reducer, {
todos: []
});

Expand All @@ -27,7 +27,7 @@ const specificEnhencer: StoreEnhancer<State> = next => next;
const storeWithGenericEnhancer: Store<State> = createStore(reducer, genericEnhancer);
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, specificEnhencer);

const storeWithInitialStateAndEnhancer: Store<State> = createStore(reducer, {
const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(reducer, {
todos: []
}, genericEnhancer);

Expand Down