Skip to content

Commit

Permalink
Merge pull request #3507 from natrim/patch-3
Browse files Browse the repository at this point in the history
[RFR] createAdminStore - set initialState on logout
  • Loading branch information
fzaninotto authored Aug 20, 2019
2 parents f8a8b9f + b549c1b commit 875d719
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
29 changes: 29 additions & 0 deletions docs/Admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,35 @@ const App = () => (

The `initialState` prop lets you pass preloaded state to Redux. See the [Redux Documentation](http://redux.js.org/docs/api/createStore.html#createstorereducer-preloadedstate-enhancer) for more details.

It accepts either a function or an object:

```jsx
const initialState = {
theme: 'dark',
grid: 5,
};

const App = () => (
<Admin initialState={initialState} dataProvider={simpleRestProvider('http://path.to.my.api')}>
// ...
</Admin>
);
```

```jsx
const initialState = () => ({
theme: localStorage.getItem('theme'),
grid: localStorage.getItem('grid'),
});

const App = () => (
<Admin initialState={initialState} dataProvider={simpleRestProvider('http://path.to.my.api')}>
// ...
</Admin>
);
```


## `history`

By default, react-admin creates URLs using a hash sign (e.g. "myadmin.acme.com/#/posts/123"). The hash portion of the URL (i.e. `#/posts/123` in the example) contains the main application route. This strategy has the benefit of working without a server, and with legacy web browsers. But you may want to use another routing strategy, e.g. to allow server-side rendering.
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/CoreAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ConnectedRouter } from 'connected-react-router';

import AuthContext from './auth/AuthContext';
import DataProviderContext from './dataProvider/DataProviderContext';
import createAdminStore from './createAdminStore';
import createAdminStore, { InitialState } from './createAdminStore';
import TranslationProvider from './i18n/TranslationProvider';
import CoreAdminRouter from './CoreAdminRouter';
import {
Expand Down Expand Up @@ -48,7 +48,7 @@ export interface AdminProps {
dataProvider: DataProvider;
history: History;
i18nProvider?: I18nProvider;
initialState?: object;
initialState?: InitialState;
loading: ComponentType;
locale?: string;
loginPage: LoginComponent | boolean;
Expand Down
15 changes: 12 additions & 3 deletions packages/ra-core/src/createAdminStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ interface Window {
__REDUX_DEVTOOLS_EXTENSION__?: () => () => void;
}

export type InitialState = object | (() => object);

interface Params {
dataProvider: DataProvider;
history: History;
authProvider?: AuthProvider;
customReducers?: any;
customSagas?: any[];
i18nProvider?: I18nProvider;
initialState?: object;
initialState?: InitialState;
locale?: string;
}

Expand All @@ -44,7 +46,14 @@ export default ({
);

const resettableAppReducer = (state, action) =>
appReducer(action.type !== CLEAR_STATE ? state : undefined, action);
appReducer(
action.type !== CLEAR_STATE
? state
: typeof initialState === 'function'
? initialState()
: initialState,
action
);
const saga = function* rootSaga() {
yield all(
[
Expand All @@ -58,7 +67,7 @@ export default ({

const store = createStore(
resettableAppReducer,
initialState,
typeof initialState === 'function' ? initialState() : initialState,
compose(
applyMiddleware(sagaMiddleware, routerMiddleware(history)),
typeof typedWindow !== 'undefined' &&
Expand Down

0 comments on commit 875d719

Please sign in to comment.