Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@ovidb/redux-dryer
Advanced tools
This library uses the strategy pattern and removes the boilerplate from your workflow.
Instead of having to write a lot of boilerplate code you basically only have to define your reducers (strategies) for updating your store.
The actions are automatically generated for you by the library when you initialize the reducer.
The library also uses Immer, which makes state updates cleaner and easier to reason about because you can mutate the objects directly instead of having the spread them.
npm install @ovidb/redux-dryer
wallet-dryer.ts
import { ActionPayload as AP, reduxDryer } from 'redux-dryer';
interface WalletState {
balance: number;
btc: number;
loading: boolean;
}
const initialState: WalletState = {
loading: false,
balance: 0,
btc: 0,
};
export type Payloads = {
SetBalance: number;
Amount: number;
SetBTCRate: number;
Loading: boolean;
};
const { reducer: walletReducer, actions } = reduxDryer({
initialState,
namespace: 'wallet',
reducers: {
// the name of the action will become the action type
setBalance: (state, action: AP<Payloads['SetBalance']>) => {
// Even though this looks like we are mutating we are not because
// we can update state directly with [Immer](https://github.com/immerjs/immer)
state.balance = action.payload;
},
depositAmount: (state, action: AP<Payloads['Amount']>) => {
state.balance += action.payload;
},
withdrawAmount: (state, action: AP<Payloads['Amount']>) => {
state.balance -= action.payload;
},
convertToBTC: (state, action: AP<Payloads['SetBTCRate']>) => {
state.btc = state.balance / action.payload;
},
setIsLoading: (state, action: AP<Payloads['Loading']>) => {
state.loading = action.payload;
},
},
});
export { walletReducer, actions };
redux-thunk
bitcoin-thunks.ts
import { actions } from './wallet-dryer';
import { AppState } from './reducers';
import { ThunkAction } from 'redux-thunk';
import { Action } from 'redux';
export interface CoinDeskCurrentPriceResponse {
bpi: { USD: { rate: string } };
}
const getRate = (json: CoinDeskCurrentPriceResponse) =>
parseFloat(parseInt(json.bpi.USD.rate.split(',').join(''), 10).toFixed(6));
export const toBTC = (): ThunkAction<
Promise<>,
AppState,
number,
Action<string>
> => async (dispatch, getState) => {
dispatch(actions.setIsLoading(true));
fetch('https://api.coindesk.com/v1/bpi/currentprice.json')
.then(r => r.json())
.then(json => {
dispatch(actions.setIsLoading(false));
dispatch(actions.convertToBTC(getRate(json)));
});
};
root-reducer.ts
import { combineReducers } from 'redux';
import { walletReducer } from './wallet-dryer';
const rootReducer = combineReducers({
wallet: walletReducer,
});
export type AppState = ReturnType<typeof rootReducer>;
export default rootReducer;
That's all you need to do, and now your reducer will listen and respond to your actions
App.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import rootReducer from './root-reducer';
import Wallet from './wallet-connected';
const store = createStore(reducers, applyMiddleware(...[thunk, logger]));
ReactDOM.render(
<Provider store={store}>
<Wallet />
</Provider>,
document.getElementById('root')
);
wallet.tsx
import React from 'react';
import { connect } from 'react-redux';
import { actions, WalletState } from './wallet-dryer';
import { fetchBTCRate } from './bitcoin-thunks';
import { AppState } from './reducers';
const mapStateToProps = ({ wallet }: AppState) => ({
...wallet,
});
const mapDispatchToProps = {};
interface Wallet {
loading: boolean;
balance: number;
btc: number;
actions: ReturnType;
}
export const Wallet: FC<Wallet> = ({ balance, loading, btc, ...actions }) => {
return (
<div>
<div>USD Balance: {balance}</div>
<div>Bitcoin Balance: {btc}</div>
<button onClick={() => actions.depositAmount(200)}>+200</button>
<button onClick={() => actions.withdrawAmount(100)}>-100</button>
<button onClick={() => actions.setBalance(10000000000)}>
I want to be Billionaire
</button>
<button onClick={() => actions.toBTC(balance)}>To BTC</button>
{loading && 'Loading...'}
</div>
);
};
export default connect(
({ wallet, bitcoin }) => ({ wallet, bitcoin }),
{
...actions,
...thunks,
}
)(Wallet);
FAQs
Redux utilities to keep your reducers small and DRY
We found that @ovidb/redux-dryer 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.