redux-actions-ts
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

npm downloads npm version

Redux-Actions-TS

⚛️ Redux-Actions with TypeScript helper package

How to use it?

Install

yarn add redux-actions-ts

Create typed and async actions

import { createAsyncActions, createTypedAction } form 'redux-actions-ts'
// async example
const {
  request: signInRequest,
  success: signInSuccess,
  failure: signInFailure,
} = createAsyncActions<Credentials, User>('SIGN_IN')
 
// Create your payload type somewhere
// type Credentials = { username: string, password: string }
// type User = { name: string }
 
// just typed action (ie. not async)
const signOut = createTypedAction<null>('SIGN_OUT')

Handle actions with reducer

import { handleTypedActions, createTypedHandler } form 'redux-actions-ts'
 
const signIn = handleTypedActions(
  [
    createTypedHandler(signInSuccess, signInSuccessReducer),
    createTypedHandler(signInFailure, signInFailureReducer),
    createTypedHandler(signOut, signOutReducer),
  ],
  {
    user: {
      isAuthenticated: false
    }
  },
)
// later combine your reducers...

Dispatch your action (with react-redux-dispatch-async if you will 👍 )

import React from 'react'
import { useDispatchAsync } from 'react-redux-dispatch-async'
 
export default function MyUserInterface({ id }: { id: string }) {
  // 👉 pass action and arguments into the array
  const response = useDispatchAsync(getUserActionRequest, [id])
 
  switch (response.status) {
    case 'loading':
      return <AppLoader />
    case 'error':
      return <Text>{response.error.message}</Text>
    case 'success':
      return <User {...response.result} />
    case 'timeout':
      return <Text>{'timeout ¯\\_(ツ)_//¯'}</Text>
    case 'canceled':
      return <Text>{'canceled ¯\\_(ツ)_//¯'}</Text>
    default:
      return null
  }
}

👉 react-redux-dispatch-async

Local Development

Below is a list of commands you will probably find useful.

npm start or yarn start

Runs the project in development/watch mode. Your project will be rebuilt upon changes. TSDX has a special logger for you convenience. Error messages are pretty printed and formatted for compatibility VS Code's Problems tab.

Your library will be rebuilt if you make edits.

npm run build or yarn build

Bundles the package to the dist folder. The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).

npm test or yarn test

Runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit.

Hire an expert!

Looking for a ReactNative freelance expert with more than 14 years of experience? Contact Xavier from his website!

Readme

Keywords

none

Package Sidebar

Install

npm i redux-actions-ts

Weekly Downloads

7

Version

0.1.1

License

MIT

Unpacked Size

29 kB

Total Files

12

Last publish

Collaborators

  • xcarpentier