Skip to content
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

New global state methods to support SSR rehydration #2

Merged
merged 4 commits into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,32 @@ dispatch(changeLocation(), "Location change")
```
You might want to dispatch a global action that is **NOT** associated with any store. The action will in this case just be shown as `Location change`.

### `getGlobalState()`
```javascript
import { getGlobalState } from 'laco'

getGlobalState()
```
Returns the global object that holds every state - mostly used for [rehydration](https://github.com/deamme/laco#Rehydration) when doing server-side rendering (SSR).

### `resetGlobalState()`
```javascript
import { resetGlobalState } from 'laco'

resetGlobalState()
```
Resets the global state to an empty object.

### `replaceGlobalState()`
```javascript
import { replaceGlobalState } from 'laco'

const newGlobalState = { 0: { test: true } }

replaceGlobalState(newGlobalState)
```
Replaces the global state completely - mostly used for [rehydration](https://github.com/deamme/laco#Rehydration) when doing server-side rendering (SSR).

### `<Subscribe />`
#### Props
- `to` - Array of stores you want to subscribe to
Expand All @@ -151,6 +177,19 @@ The `Subscribe` component is making use of the new render prop idea. Related art
- [Apollo Query Component](https://dev-blog.apollodata.com/whats-next-for-react-apollo-4d41ba12c2cb)
- [Use a render prop!](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce)

## Rehydration
When doing server-side rendering (SSR) it's important to preserve the state from the server to the client.

Please follow [this](https://redux.js.org/recipes/serverrendering) Redux guide.

On the server: Instead of doing `store.getState()` you will just use `getGlobalState()`.

On the client: Instead of doing `createStore(counterApp, preloadedState)` you can do `replaceGlobalState(preloadedState)`

Keep in mind that trying to do SSR rehydration can introduce JS injections if you don't do it right.

The Redux guide solves it by doing `JSON.stringify(preloadedState).replace(/</g, '\\u003c')`. For another solution look [here](https://github.com/deamme/laco/pull/2#issuecomment-417880218).

## Testing
Testing using [tape](https://github.com/substack/tape):
```javascript
Expand Down
12 changes: 12 additions & 0 deletions packages/laco/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ export function dispatch(value: any, info: string) {
}
return value
}

export function getGlobalState() {
return STORE
}

export function resetGlobalState() {
STORE = {}
}

export function replaceGlobalState(state: Object) {
STORE = state
}
13 changes: 12 additions & 1 deletion packages/laco/tests/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as test from 'tape'
import { Store } from '../dist'
import { Store, getGlobalState, resetGlobalState, replaceGlobalState } from '../dist'

test('Correct store index', t => {
const FirstStore = new Store({ test: true })
Expand Down Expand Up @@ -138,3 +138,14 @@ test('TestStore actions with condition', t => {

t.end()
})

test('Global state', t => {
resetGlobalState()
t.assert(JSON.stringify(getGlobalState()) === JSON.stringify({}))

const newGlobalState = { 0: { test: true }}
replaceGlobalState(newGlobalState)
t.assert(JSON.stringify(getGlobalState()) === JSON.stringify(getGlobalState()))

t.end()
})