diff --git a/react-router-redux/react-router-redux-2.x-tests.ts b/react-router-redux/react-router-redux-2.x-tests.ts new file mode 100644 index 0000000000..9b3d59fbb1 --- /dev/null +++ b/react-router-redux/react-router-redux-2.x-tests.ts @@ -0,0 +1,20 @@ +/// +/// +/// + + + +import { createStore, combineReducers, applyMiddleware } from 'redux'; +import { browserHistory } from 'react-router'; +import { syncHistory, routeReducer } from 'react-router-redux'; + +const reducer = combineReducers({ routing: routeReducer }); + +// Sync dispatched route actions to the history +const reduxRouterMiddleware = syncHistory(browserHistory); +const createStoreWithMiddleware = applyMiddleware(reduxRouterMiddleware)(createStore); + +const store = createStoreWithMiddleware(reducer); + +// Required for replaying actions from devtools to +reduxRouterMiddleware.listenForReplays(store); diff --git a/react-router-redux/react-router-redux-2.x.d.ts b/react-router-redux/react-router-redux-2.x.d.ts new file mode 100644 index 0000000000..a51766fcf9 --- /dev/null +++ b/react-router-redux/react-router-redux-2.x.d.ts @@ -0,0 +1,48 @@ +// Type definitions for react-router-redux v2.x - v3.x +// Project: https://github.com/rackt/react-router-redux +// Definitions by: Isman Usoh , Noah Shipley , Dimitri Rosenberg +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +declare namespace ReactRouterRedux { + import R = Redux; + import H = HistoryModule; + + const TRANSITION: string; + const UPDATE_LOCATION: string; + + const push: PushAction; + const replace: ReplaceAction; + const go: GoAction; + const goBack: GoForwardAction; + const goForward: GoBackAction; + const routeActions: RouteActions; + + type LocationDescriptor = H.Location | H.Path; + type PushAction = (nextLocation: LocationDescriptor) => void; + type ReplaceAction = (nextLocation: LocationDescriptor) => void; + type GoAction = (n: number) => void; + type GoForwardAction = () => void; + type GoBackAction = () => void; + + interface RouteActions { + push: PushAction; + replace: ReplaceAction; + go: GoAction; + goForward: GoForwardAction; + goBack: GoBackAction; + } + interface HistoryMiddleware extends R.Middleware { + listenForReplays(store: R.Store, selectLocationState?: Function): void; + unsubscribe(): void; + } + + function routeReducer(state?: any, options?: any): R.Reducer; + function syncHistory(history: H.History): HistoryMiddleware; +} + +declare module "react-router-redux" { + export = ReactRouterRedux; +} diff --git a/react-router-redux/react-router-redux-tests.ts b/react-router-redux/react-router-redux-tests.ts index 98fcdca1be..baaec2a9c5 100644 --- a/react-router-redux/react-router-redux-tests.ts +++ b/react-router-redux/react-router-redux-tests.ts @@ -6,15 +6,25 @@ import { createStore, combineReducers, applyMiddleware } from 'redux'; import { browserHistory } from 'react-router'; -import { syncHistory, routeReducer } from 'react-router-redux'; +import { syncHistoryWithStore, routerReducer, routerMiddleware, push, replace, go, goForward, goBack } from 'react-router-redux'; -const reducer = combineReducers({ routing: routeReducer }); +const reducer = combineReducers({ routing: routerReducer }); -// Sync dispatched route actions to the history -const reduxRouterMiddleware = syncHistory(browserHistory); -const createStoreWithMiddleware = applyMiddleware(reduxRouterMiddleware)(createStore); +// Apply the middleware to the store +const middleware = routerMiddleware(browserHistory); +const store = createStore( + reducer, + applyMiddleware(middleware) +); -const store = createStoreWithMiddleware(reducer); +// Create an enhanced history that syncs navigation events with the store +const history = syncHistoryWithStore(browserHistory, store); +history.listen(location => console.log(location) ); +history.unsubscribe(); -// Required for replaying actions from devtools to -reduxRouterMiddleware.listenForReplays(store); +// Dispatch from anywhere like normal. +store.dispatch(push('/foo')); +store.dispatch(replace('/foo')); +store.dispatch(go(1)); +store.dispatch(goForward()); +store.dispatch(goBack()); diff --git a/react-router-redux/react-router-redux.d.ts b/react-router-redux/react-router-redux.d.ts index 7248fb3afc..168161a179 100644 --- a/react-router-redux/react-router-redux.d.ts +++ b/react-router-redux/react-router-redux.d.ts @@ -1,4 +1,4 @@ -// Type definitions for react-router-redux v2.1.0 +// Type definitions for react-router-redux v4.0.0 // Project: https://github.com/rackt/react-router-redux // Definitions by: Isman Usoh , Noah Shipley , Dimitri Rosenberg // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -10,23 +10,28 @@ declare namespace ReactRouterRedux { import R = Redux; import H = HistoryModule; - const TRANSITION: string; - const UPDATE_LOCATION: string; + const CALL_HISTORY_METHOD: string; + const LOCATION_CHANGE: string; const push: PushAction; const replace: ReplaceAction; const go: GoAction; const goBack: GoForwardAction; const goForward: GoBackAction; - const routeActions: RouteActions; + const routerActions: RouteActions; type LocationDescriptor = H.Location | H.Path; - type PushAction = (nextLocation: LocationDescriptor) => void; - type ReplaceAction = (nextLocation: LocationDescriptor) => void; - type GoAction = (n: number) => void; - type GoForwardAction = () => void; - type GoBackAction = () => void; + type PushAction = (nextLocation: LocationDescriptor) => RouterAction; + type ReplaceAction = (nextLocation: LocationDescriptor) => RouterAction; + type GoAction = (n: number) => RouterAction; + type GoForwardAction = () => RouterAction; + type GoBackAction = () => RouterAction; + type RouterAction = { + type: string + payload?: any + } + interface RouteActions { push: PushAction; replace: ReplaceAction; @@ -34,13 +39,22 @@ declare namespace ReactRouterRedux { goForward: GoForwardAction; goBack: GoBackAction; } - interface HistoryMiddleware extends R.Middleware { - listenForReplays(store: R.Store, selectLocationState?: Function): void; + interface ReactRouterReduxHistory extends H.History { unsubscribe(): void; } + + interface DefaultSelectLocationState extends Function { + (state: any): any; + } - function routeReducer(state?: any, options?: any): R.Reducer; - function syncHistory(history: H.History): HistoryMiddleware; + interface SyncHistoryWithStoreOptions { + selectLocationState?: DefaultSelectLocationState; + adjustUrlOnReplay?: boolean; + } + + function routerReducer(state?: any, options?: any): R.Reducer; + function syncHistoryWithStore(history: H.History, store: R.Store, options?: SyncHistoryWithStoreOptions): ReactRouterReduxHistory; + function routerMiddleware(history: H.History): R.Middleware; } declare module "react-router-redux" {