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

1.3.5 • Public • Published

react-redux-ts

Typescript wrapper around redux and react-redux that simplifies workflow

npm i react-redux-ts

1. Simple set

Adds a common action to set any value in the store via dispatch with typechecks. Without async:

store.setStateProp({
    prop: 'myProp',
    payload: 'val'
});

And async (similar to Redux-thunk):

store.setStatePropAsync({
    prop: 'myProp',
    func: myAsyncFunction
});

Will ensure that 'payload' in SET_PROP and return value of 'func' in SET_PROP_ASYNC has the same type as property in the store.

Async version will also dispatch notifications in order:

  • start
  • success / error
  • end

Will also add both methods as props to connected react component

props.setStateProp(/* { ... } */);
props.setStatePropAsync(/* { ... } */);

2. Type actions

Allows to use type definitions as actions instead of objects.

actions.ts:
import { StateType } from '../store';
type ClearTodosAction = {
    type: 'CLEAR_TODOS';
};

type SetVisibilityFilter = {
    type: 'SET_VISIBILITY_FILTER';
    filter: StateType['filter'];
};

export type ActionTypes = ClearTodosAction | SetVisibilityFilter;
customReducer.ts:
import { ActionTypes } from '../actions';
import { StateType } from '../store';

export function customReducer(state: StateType, action: ActionTypes): StateType {
    switch (action.type) {
        case 'SET_VISIBILITY_FILTER':
            return {
                ...state,
                filter: action.filter
            }
        case 'CLEAR_TODOS':
            return {
                ...state,
                todos: []
            }
        default:
            return state;
    }
};
store.ts:
import { createStore } from 'react-redux-ts';
import { ActionTypes } from './actions';
import { customReducer } from './customReducer';

class State {
    todos: string[] = [];
    filter: 'SHOW_ALL' | 'HIDE_ALL' = 'SHOW_ALL';
    myProp: string = '';
}

// All arguments of createStore are optional. It accepts your root reducer, initial state, 
// boolean for whether or not use devtools, array of middlewares.
export const store = createStore<State, ActionTypes<State>>(
    customReducer, new State());

export type DispatchType = typeof store.dispatch;
export type StoreAction = ReturnType<typeof store.dispatch>;
export type StateType = typeof State;
AppComponent.tsx:
import { connect, PropsType } from 'react-redux-ts';
import { StateType, DispatchType } from './store.ts';

type OwnPropsType = {};

const mapStateToProps = (state: StateType, ownProps: OwnPropsType) => {
    return {
        myProp: state.myProp
    }
}
const mapDispatchToProps = (dispatch: DispatchType, ownProps: OwnPropsType) => {
    return {
        clearTodos: () => dispatch({ type: 'CLEAR_TODOS' })
    }
}
type AppComponentPropsType = PropsType<StateType, OwnPropsType, 
    ReturnType<typeof mapStateToProps>, ReturnType<typeof mapDispatchToProps>>;

const AppComponent = (props: AppComponentPropsType) => {
    return (
        <div
            onClick={() => {
                props.setStateProp({
                    prop: 'myProp',
                    payload: 'val'
                });
                props.clearTodos();
            }}
        >{props.myProp}</div>
    )
}

const connectedComponent = connect(mapStateToProps, mapDispatchToProps)(AppComponent);
export default connectedComponent;
index.tsx:
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux-ts'
import store from './store'
import AppComponent from './AppComponent'

const rootElement = document.getElementById('root');
ReactDOM.render(
    <Provider store={store}>
        <AppComponent />
    </Provider>,
    rootElement
);

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.3.53latest

Version History

VersionDownloads (Last 7 Days)Published
1.3.53
1.3.40
1.3.30
1.3.20
1.3.10
1.3.00
1.2.80
1.2.70
1.2.60
1.2.50
1.2.40
1.2.30
1.2.20
1.2.10
1.2.00
1.1.60
1.1.50
1.1.40
1.1.30
1.1.20
1.1.10
1.1.01
1.0.120
1.0.111
1.0.100
1.0.90
1.0.80
1.0.71
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10

Package Sidebar

Install

npm i react-redux-ts

Weekly Downloads

6

Version

1.3.5

License

MIT

Unpacked Size

35.8 kB

Total Files

14

Last publish

Collaborators

  • elrix