Skip to content

Commit

Permalink
Improve handling of rehydration of state - now available in action pa…
Browse files Browse the repository at this point in the history
…yload.
  • Loading branch information
zewish committed Aug 16, 2023
1 parent de5972d commit dcc27b5
Show file tree
Hide file tree
Showing 7 changed files with 2,383 additions and 9,590 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"demo-web/**",
"dist/**",
"lib/**",
"es/**"
"es/**",
"rollup.config.mjs"
],
"rules": {
"comma-dangle": 0,
Expand Down
38 changes: 20 additions & 18 deletions LEGACY-USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ Legacy usage - web
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import { rememberReducer, rememberEnhancer } from 'redux-remember';

const myStateIsRemembered = (state = '', { type, payload }) => {
switch (type) {
const myStateIsRemembered = (state = '', action) => {
switch (action.type) {
case 'SET_TEXT1':
return payload;
return action.payload;

default:
return state;
}
};

const myStateIsForgotten = (state = '', { type, payload }) => {
switch (type) {
const myStateIsForgotten = (state = '', action) => {
switch (action.type) {
case 'SET_TEXT2':
return payload;
return action.payload;

default:
return state;
Expand Down Expand Up @@ -62,20 +62,20 @@ import AsyncStorage from '@react-native-community/async-storage';
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import { rememberReducer, rememberEnhancer } from 'redux-remember';

const myStateIsRemembered = (state = '', { type, payload }) => {
switch (type) {
const myStateIsRemembered = (state = '', action) => {
switch (action.type) {
case 'SET_TEXT1':
return payload;
return action.payload;

default:
return state;
}
};

const myStateIsForgotten = (state = '', { type, payload }) => {
switch (type) {
const myStateIsForgotten = (state = '', action) => {
switch (action.type) {
case 'SET_TEXT2':
return payload;
return action.payload;

default:
return state;
Expand Down Expand Up @@ -122,13 +122,13 @@ const defaultState = {
persisted: false
};

export default (state = defaultState, { type, payload }) => {
switch (type) {
const changeMeReducer = (state = defaultState, action) => {
switch (action.type) {
case REMEMBER_REHYDRATED:
// state gets rehydrated from storage
// state == { changeMe: 123 }
// payload is Rehydrated Root State
return {
...state,
...changeMe: action.payload.changeMeReducer?.changeMe || null,
rehydrated: true
};

Expand All @@ -148,11 +148,13 @@ export default (state = defaultState, { type, payload }) => {

return {
...state,
changeMe: payload
changeMe: action.payload
};

default:
return state;
}
};
}

export default changeMeReducer;
```
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Usage - inside a reducer
------------------------

```ts
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { createSlice, createAction, PayloadAction } from '@reduxjs/toolkit';
import { REMEMBER_REHYDRATED, REMEMBER_PERSISTED } from 'redux-remember';

type InitialState = {
Expand All @@ -162,8 +162,8 @@ const initialState: InitialState = {
persisted: false
};

const usageInsideReducer = createSlice({
name: 'usage-inside-reducer-slice',
const myReducer = createSlice({
name: 'my-reducer',
initialState,
reducers: {
someAction(state, action: PayloadAction<{ changeMe: any }>) {
Expand All @@ -175,11 +175,13 @@ const usageInsideReducer = createSlice({
}
},
extraReducers: (builder) => builder
.addCase(REMEMBER_REHYDRATED, (state, action) => {
state.changeMe = (action as PayloadAction<InitialState>).payload.changeMe;
.addCase(createAction<{ myReducer?: InitialState }>(REMEMBER_REHYDRATED), (state, action) => {
// @INFO: action.payload.myReducer => rehydrated state of this reducer or "undefined" during the first run
state.changeMe = action.payload.myReducer?.changeMe || null;
state.rehydrated = true;
})
.addCase(REMEMBER_PERSISTED, (state) => {
.addCase(createAction<{ myReducer?: InitialState }>(REMEMBER_PERSISTED), (state, action) => {
// @INFO: action.payload.myReducer => persisted state of this reducer or "undefined" in case this reducer is not persisted
state.rehydrated = false;
state.persisted = true;
})
Expand Down

0 comments on commit dcc27b5

Please sign in to comment.