Last active
February 15, 2021 01:02
-
-
Save ducin/f8d4b1fffcc84c1bf16876dd8b6876d9 to your computer and use it in GitHub Desktop.
redux setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { INC } from './constants'; | |
export const Inc = () => ({ | |
type: INC as typeof INC | |
}) | |
export type Actions = | |
| ReturnType<typeof Inc> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const INC = "INC" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type AppState = { | |
count: number | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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)