react-redux-typed-hooks

1.0.1 • Public • Published

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'

Package Sidebar

Install

npm i react-redux-typed-hooks

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

2.96 kB

Total Files

6

Last publish

Collaborators

  • sarimarton