react-redux-procedures
v1.4.0
Published
A wrapper for react-redux's connect to allow execution of multiple actions in a single procedure
Downloads
17
Keywords
Readme
react-redux-procedures
A wrapper for react-redux's connect to allow execution of multiple actions in a single procedure
How it works
The procedures are mapped into the three arguments of the basic connect method of react-redux, so that you get props of dispatchable "procedures". Procedures only take arguments and are already stateful and able to dispatch actions. This is really useful if you have actions that are always executed after or next to each other, or if you use actions that depend a lot on your apps State.
API
createProcedure
// procedure file
import { createProcedure } from "react-redux-procedures";
type State = { someStateVal: any }; // to be filled in, if working with flow
type Params = {};
export const procedure = createProcedure(
dispatch => ({ someStateVal }: State) => ({ }: Params): Promise<any> => {
// this is where the magic happens.
// you can dispatch actions, they may be dependent on each other.
dispatch(someAction());
dispatch(someOtherAction);
return dispatch(somePromiseAction()).then(() => 'Yay!'):
},
(state: AppState) => ({
someStateVal: state.val
})
);
IMPORTANT: Don't forget that the state is already bound when you call the procedure. this means it won't change after dispatching actions internally and may have already changed when you reach a promise result. Procedures should only be dependent on the state at the begin of the procedure.
connectProcedures(
mapProceduresToProps: { [key]: Procedure },
mapStateToProps?,
mapDispatchToProps?,
mergeProps?,
options?
)
The optional Arguments are just your standard connect arguments that will get applied by connectProcedures so you don't have to wrap a component in connectProcedures and a standard connect as well.
// component file
import { connectProcedures, type ProcedureDispatcher } from 'react-redux-procedures';
import { someImportedProcedure } from 'somewhere';
type Props = {
someProc: ProcedureDispatcher<typeof someImportedProcedure>,
};
const Component = ({ someProc }: Props) =>
<span onClick={() => someProc()}> I dispatch a procedure! </span>;
const mapProceduresToProps = {
someProc: someImportedProcedure,
};
export default connectProcedures(mapProceduresToProps)(Component);
prepareProcedure(Procedure, Dispatch, State)
// file where you need to call a procedure manually
import { prepareProcedure } from "react-redux-procedures";
import { someImportedProcedure } from "somewhere";
const prepared = prepareProcedure(someImportedProcedure, reduxStore.dispatch, reduxStore.getState());
prepared(/* params */);
Exploration of what Procedures are via their types
Procedure and StatelessProcedure
type Procedure<State, Params, Result, AppState> = {
(dispatch: Dispatch<any>): State => Params => Result,
mapStateToProcState: AppState => S,
};
So, a procedure is something that
- is dependent of 4 (type) arguments: State, Params, Result and AppState
- is mainly a curried function that takes a dispatch function, some procedure-specific state, and params
- has a property
mapStateToProcState
which takes the AppState and returns the procedure-specific state. - has a result of type Promise (this is not in the procedure type above for simplicity, but is expected in the createProcedure function).
There is also a stateless variant, seen below.
type StatelessProcedure<Params, Result> = {
(dispatch: Dispatch<any>): () => Params => Result,
};
ProcedureDispatcher
As the name suggests, this is just a helper type that takes the Params => Result
bit of a given procedure. The type is
simplified to not confuse you with flow weirdness, just know that Params and Result are inferred from the given
Procedure.
type ProcedureDispatcher<Procedure|StatelessProcedure> = Params => Result;