- Notifications
You must be signed in to change notification settings - Fork1
cimdalli/redux-ts
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Utils to define react redux reducer/action in typescript.
For breaking changes you can take lookCHANGELOG
npminstall--saveredux-ts
Create redux store with builder pattern.
import{StoreBuilder}from'redux-ts'conststore=newStoreBuilder<StoreState>().withInitialState({test:true}).withReducer("reducer",reducer).withDevTools()// enable chrome devtools.build();}
Action declaration can be done with'createAction' function which takes actiontype as parameter.
import{createAction}from'redux-ts'interfaceLoginPayload{username:stringpassword:string}interfaceSetTokenPayload{token?:string}exportconstLogin=createAction<LoginPayload>('Login')exportconstLogout=createAction('Logout')exportconstSetToken=createAction<SetTokenPayload>('SetToken')
Reducers are consumers for actions to change application state. Difference from original redux implementation is inredux-ts reducers can also dispatch another action asynchronously. Each reducer method should return state value even it doesn't change it. Async dispatch operations will be handled after original dispatch cycle is finished.
import{ReducerBuilder}from'redux-ts'import{Login,Logout,SetToken}from'../actions'import{push}from'react-router-redux'consttokenKey='auth'exportinterfaceAuthState{token?:string}exportconstauthReducer=newReducerBuilder<AuthState>().init({token:localStorage.getItem(tokenKey)||undefined,}).handle(Login,(state,action,dispatch)=>{const{ username, password}=action.payloadfetch(`https://server.com/login?u=${username}&p=${password}`).then(x=>x.json()).then(data=>{/* * When server respond with token, another action is dispatching. */dispatch(SetToken(data.token))dispatch(push('/dashboard'))})returnstate}).handle(Logout,(state,action,dispatch)=>{dispatch(SetToken({token:undefined}))dispatch(push('/dashboard'))returnstate}).handle(SetToken,(state,action)=>{consttoken=action.payload.tokenif(token){localStorage.setItem(tokenKey,token)}else{localStorage.removeItem(tokenKey)}return{ ...state, token,}})
connect method is part of redux library and allows you to connect your react components with redux store. Although you can use your own implementation, this library provides you some syntactic sugar to make it easy.
import*asReactfrom'react'import{ChangeTheme}from'../actions/layout.actions'import{Logout}from'../actions/auth.actions'import{TopBar}from'../components/topBar'import{connect}from'react-redux'import{mapDispatchToProps,StateToProps}from'redux-ts'constmapStoreToProps:StateToProps<StoreState>=map=>mapconststoreProps=mapStoreToProps(store=>({useDarkTheme:!!store.layout.useDarkTheme,}))constdispatchProps=mapDispatchToProps({ Logout, ChangeTheme,})typeMainProps=ReturnType<typeofdispatchProps>&ReturnType<typeofstoreProps>constMainContainer:React.SFC<MainProps>=({ children, ...rest})=>(<div><TopBar{...rest}/>{children}</div>)exportconstMain=connect(storeProps,dispatchProps)(MainContainer)
MIT
About
Utils to define react redux reducers/actions in typescript.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.