react-redux-typed-hooks
v1.0.1
Published
Typed hooks for react-redux
Downloads
2
Maintainers
Readme
react-redux-typed-hooks
Typed hooks for react-redux. Read more about it here.
Usage:
- Define your store and store event types (store events are objects which are passed to the
dispatch
function). - Get the hooks from the factory provided by this package, and export them.
- Enjoy.
Example:
// store/index.ts
import { createStore, combineReducers } from 'redux'
import getHooks from 'react-redux-typed-hooks'
import fruits, { FruitStore, FruitStoreEvent } from './fruits'
import others, { OtherStore, OtherStoreEvent } from './others'
type Event = FruitStoreEvent | OtherStoreEvent
interface Store {
fruits: FruitStore;
others: OtherStore;
}
export const { useSelector, useDispatch } = getHooks<Store, Event>()
export default createStore(
combineReducers({
fruits,
others
})
)
In your component files:
// view/component.tsx
// import { useDispatch, useSelector } from 'react-redux' // WRONG
import { useSelector, useDispatch } from '../store'