redux setup
import { INC } from './constants'; | |
export const Inc = () => ({ | |
type: INC as typeof INC | |
}) | |
export type Actions = | |
| ReturnType<typeof Inc> |
import React from 'react' | |
import { connect } from 'react-redux' | |
import { AppState } from './state'; | |
export const ReduxComponent = connect( | |
(state: AppState) => ({ | |
count: state.count | |
}), | |
(dispatch) => ({ | |
// | |
}))((props: { count: number }) => <span>{props.count}</span>) |
export const INC = "INC" |
import { Observable, empty, interval } from "rxjs"; | |
import { ActionsObservable, ofType, combineEpics, StateObservable } from "redux-observable"; | |
import { switchMapTo, tap, mapTo, withLatestFrom } from "rxjs/operators"; | |
import { Actions, Inc } from "./actions"; | |
import { AppState } from "./state"; | |
export const logIncEpic = (action$: ActionsObservable<Actions>, state$: StateObservable<AppState>) => action$.pipe( | |
ofType("INC"), | |
withLatestFrom(state$), | |
tap(([action, state]) => console.log(action.type, state)), | |
switchMapTo(empty()), | |
) | |
export const each5secEpic = (action$: ActionsObservable<Actions>) => interval(5000).pipe( | |
mapTo(Inc()) | |
) | |
export const rootEpic = combineEpics( | |
logIncEpic, | |
each5secEpic | |
) |
import { AppState } from './state'; | |
import { Actions } from './actions'; | |
const initialState: AppState = { | |
count: 1 | |
} | |
export const rootReducer = (state = initialState, action: Actions) => { | |
switch(action.type) { | |
case "INC": | |
return { ...state, count: state.count + 1 } | |
default: | |
return state | |
} | |
} |
import { createStore, applyMiddleware, Store } from 'redux' | |
import { composeWithDevTools } from 'redux-devtools-extension' | |
import thunk from 'redux-thunk' | |
import { createEpicMiddleware, EpicMiddleware } from 'redux-observable' | |
import { rootReducer } from './reducers' | |
import { rootEpic } from './epics' | |
const epicMiddleware: EpicMiddleware<Actions, Actions, AppState> = createEpicMiddleware(); | |
const middleware = [thunk, epicMiddleware] | |
import { AppState } from './state'; | |
import { Actions } from './actions'; | |
const composeEnhancers = composeWithDevTools({ | |
name: "IT Corpo React App" | |
}); | |
export const getStore = (): Store<AppState> => { | |
const store = createStore(rootReducer, composeEnhancers( | |
applyMiddleware(...middleware), | |
// other store enhancers... | |
)) | |
epicMiddleware.run(rootEpic); | |
return store; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
wejdź do głównego folderu projektu (
itcorpo-react-app
)(ostatnia komenda zakłada że masz unixowego basha (lub git basha, jeśli jesteś na windowsie)