The types described in this document require downloading via npm to view. Only parameter types and return values are outlined here. Feel free to contribute to improving the docs if you have time.
createStore
Create a store plugin, accepts same args as Redux's configureStore
- supports reducer
, middleware
, devTools
, preloadedState
, enhancers
.
Type Declaration
declare function createStore<
S extends object,
A extends Action = AnyAction,
M extends Middlewares<S> = [ThunkMiddlewareFor<S>]
>(options: ConfigureStoreOptions<S, A, M>): Plugin<EnhancedStore<S, A, M>>
Example
import { createStore } from '@gyron/redux'
export default createStore({
reducer: {},
})
useSelector
Get state from store, supports string or function. Function is passed current state.
Type Declaration
declare function useSelector(sliceName?: string | ((state: any) => any)): any
Example
import { useSelector, FC } from '@gyron/redux'
const App = FC(() => {
const state = useSelector()
return <div>{state.value}</div>
})
useStore
Get the store object.
Type Declaration
declare function useStore(): StorePlugin<any>
Example
import { useStore, FC } from '@gyron/redux'
const App = FC(() => {
const { state } = useStore()
return <div>{state.name}</div>
})
useDispatch
Returns dispatch to trigger state changes.
Type Declaration
declare function useDispatch(): (
action: Partial<{
payload: any
type: any
}>
) => void
Example
import { useDispatch, FC } from '@gyron/redux'
const App = FC(() => {
const dispatch = useDispatch()
dispatch({ type: 'counter/increment' })
return <div>{state.name}</div>
})
createAction
Create an action trigger, same as Redux createAction
. See createAction.
Type Declaration
declare function createAction<P = void, T extends string = string>(
type: T
): PayloadActionCreator<P, T>
Example
import { createAction } from '@gyron/redux'
const increment = createAction('counter/increment')
increment()
// { type: 'counter/increment' }
createReducer
Simplify reducer creation. See createReducer.
Type Declaration
declare function createReducer<S extends NotFunction<any>>(
initialState: S | (() => S),
builderCallback: (builder: ActionReducerMapBuilder<S>) => void
): ReducerWithInitialState<S>
Example
import { createAction, createReducer } from '@gyron/redux'
const increment = createAction('counter/increment')
const initialState = { value: 0 }
const counterReducer = createReducer(initialState, (builder) => {
builder.addCase(increment, (state, action) => {
state.value++
})
})
createSlice
Create a slice of state logic. See createSlice.
Type Declaration
declare function createSlice<
State,
CaseReducers extends SliceCaseReducers<State>,
Name extends string = string
>(
options: CreateSliceOptions<State, CaseReducers, Name>
): Slice<State, CaseReducers, Name>
Example
import { createSlice } from '@gyron/redux'
const counter = createSlice({
name: 'counter',
initialState: {
count: 0,
},
reducers: {
increment(state) {
state.count++
},
},
})
createAsyncThunk
See createAsyncThunk.
createEntityAdapter
See createEntityAdapter.