Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
react-redux-typescript
Advanced tools
Readme
Redux helpers for Type-safety (action types, action creators, reducers)
export class ActionCreator<T, P> {
readonly type: T;
readonly payload: P;
constructor(type: T) { this.type = type; }
create = (payload: P) => ({ type: this.type, payload });
}
// Example:
import { ActionCreator } from 'react-redux-typescript';
export const ActionCreators = {
IncreaseCounter: new ActionCreator<'IncreaseCounter', number>('IncreaseCounter'),
ChangeBaseCurrency: new ActionCreator<'ChangeBaseCurrency', string>('ChangeBaseCurrency'),
};
store.dispatch(ActionCreators.IncreaseCounter.create(4)); // { type: "IncreaseCounter", payload: 4 }
store.dispatch(ActionCreators.ChangeBaseCurrency.create('USD')); // { type: "ChangeBaseCurrency", payload: 'USD' }
// Action Types
type Action = typeof ActionCreators[keyof typeof ActionCreators];
// { type: "IncreaseCounter", payload: number } | { type: "ChangeBaseCurrency", payload: string }
// Reducer vvvvvv
export default function reducer(state: State = initialState, action: Action): State {
if (action.type === ActionCreators.IncreaseCounter.type) {
state.counter = action.payload; // number
}
else if (action.type === ActionCreators.ChangeBaseCurrency.type) {
state.baseCurrency = action.payload; // string
}
...
/**
* @type EmptyAction - Empty Action Type
* @template T - Generic Type
*/
export type EmptyAction<T> = {
readonly type: T;
}
/**
* @type PayloadAction - Flux Standard Action Type
* @template T - Generic Type
* @template P - Generic Type
*/
export type PayloadAction<T, P> = {
readonly type: T;
readonly payload: P;
readonly error?: boolean;
}
export function createEmptyAction<T>(type: T): () => EmptyAction<T> {
return () => ({ type });
};
export function createPayloadAction<T, P>(type: T): (payload: P) => PayloadAction<T, P> {
return (payload) => ({ type, payload });
}
// returntypeof() - extract return type of an "expression"
// this polyfill exist because TypeScript does not support this feature yet
// (tracking issue: https://github.com/Microsoft/TypeScript/issues/6606)
export function returntypeof<RT>(expression: (...params: any[]) => RT): RT {
return {} as RT;
}
// Example:
import { returntypeof } from 'react-redux-typescript';
const mapStateToProps = (state: RootState) => ({
counter: state.counter,
baseCurrency: state.baseCurrency,
currencies: CurrencyRatesSelectors.getCurrencies(state),
});
const dispatchToProps = {
increaseCounter: ActionCreators.IncreaseCounter.create,
changeBaseCurrency: ActionCreators.ChangeBaseCurrency.create,
};
// Props types inferred from mapStateToProps & dispatchToProps
const stateProps = returntypeof(mapStateToProps);
type Props = typeof stateProps & typeof dispatchToProps;
type State = {};
class CurrencyConverterContainer extends React.Component<Props, State> {
...
}
export default connect(mapStateToProps, dispatchToProps)(CurrencyConverterContainer);
Copyright (c) 2016 Piotr Witek piotrek.witek@gmail.com (http://piotrwitek.github.io)
FAQs
React / Redux / TypeScript Utils
The npm package react-redux-typescript receives a total of 2,176 weekly downloads. As such, react-redux-typescript popularity was classified as popular.
We found that react-redux-typescript 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.