
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
redux-thunk-actions
Advanced tools
Easily create action creators
for redux with redux-thunk.
With redux-actions you can do:
let increment = createAction('INCREMENT');
expect(increment(42)).to.deep.equal({
type: 'INCREMENT',
payload: 42
});
With redux-thunk you can do:
function myFetch() {
// instead of an object, you can return a function
return (dispatch) => {
dispatch({type: 'MY_FETCH_START'});
try {
//we can do async and then dispatch more stuff
await api.fetch();
}
catch(e) {
return dispatch({type: 'MY_FETCH_FAIL', error: e});
}
dispatch({type: 'MY_FETCH_END'});
}
}
dispatch(myFetch());
With redux-thunk-actions, you can do:
let myFetch = createActionThunk('MY_FETCH', () => api.fetch());
This will generate two of three possible actions:
You can pass both sync and async functions and the actions will be dispatched accordingly.
npm install --save redux-thunk-actions
import { createActionThunk } from 'redux-thunk-actions';
With non async functions, it will dispatch start/fail/end actions anyway.
reducer.js
case 'FETCH_SUCCEEDED':
return Object.assign({}, state, {
data: action.payload
});
You can dispatch as usual:
let fetch = createActionThunk('FETCH', () => 3);
dispatch(fetch());
assert.equal(store.getState().data, 3);
let fetch = createActionThunk('FETCH', myAsyncFunc);
// you can try/catch dispatch.
let data = await dispatch(fetch());
With promises:
let fetch = createActionThunk('FETCH', myAsyncFunc);
dispatch(fetch()).then(
data => {
console.log(data)
//state is already updated!
assert.equal(store.getState().data, data);
},
error => console.log(error)
);
reducer.js
//...
case 'FETCH_FAILED':
return Object.assign({}, state, {
started: false,
error: action.error
});
then if the action throws it fails:
let fetch = createActionThunk('FETCH', () => {
throw new Error('boom!');
});
try {
//if action is async, you can use await here!
dispatch(fetch());
}
catch(e) {
assert.equal(e.message, 'boom!');
assert.equal(getState().error, true);
}
if you want to suppress throwing exceptions and instead handle errors 100% in the reducers, pass true
as the 3rd argument
const action = createActionThunk('FETCH', () => 3, true)
Sometimes you want to send metada with your actions
To do so just return an object with payload
and meta
. This properties will be used to generate those values:
let fetch = createActionThunk('FETCH', () => ({payload: 3, meta: 4}));
dispatch(fetch());
// will dispatch:
// {type: FETCH_START});
// {type: FETCH_SUCCEEDED, payload: 3, meta: 4});
// {type: FETCH_ENDED, payload: elapsed: 1});
FAQs
redux-thunk-actions ===================
The npm package redux-thunk-actions receives a total of 706 weekly downloads. As such, redux-thunk-actions popularity was classified as not popular.
We found that redux-thunk-actions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.