Skip to content

Instantly share code, notes, and snippets.

View jamesseanwright's full-sized avatar
🤔

James Wright jamesseanwright

🤔
View GitHub Profile
@jamesseanwright
jamesseanwright / roll-own-redux-state.tsx
Created December 21, 2018 15:15
Roll Your Own Redux State
export interface State {
messages: string[];
isFormValid: boolean;
isLoadingQuote: boolean;
hasQuoteError: boolean;
}
export const defaultState: State = {
messages: [],
isFormValid: true,
@jamesseanwright
jamesseanwright / ryor-root-tree.tsx
Created December 21, 2018 15:31
Roll Your Own Redux Root Component
export default () => (
<Provider
defaultState={defaultState}
reducer={rootReducer}
>
<Status />
<MessageForm />
<MessageList />
</Provider>
);
@jamesseanwright
jamesseanwright / ryor-connect-example.tsx
Last active December 21, 2018 15:46
Roll Your Own Redux - connect Example
const mapStateToProps = ({ isFormValid, hasQuoteError, isLoadingQuote }: StateProps) => ({
isFormValid,
hasQuoteError,
isLoadingQuote,
});
const mapDispatchToProps = (dispatch: React.Dispatch<Action>) => ({
addMessage: (message: string) => dispatch(addMessage(message)),
addRonSwansonQuote: () => dispatch(addRonSwansonQuote()),
});
@jamesseanwright
jamesseanwright / ryor-statecontext.tsx
Created December 21, 2018 15:45
Roll Your Own Redux StateContext
interface Context {
state: State;
dispatch: React.Dispatch<Action>;
}
const StateContext = React.createContext<Context>({
state: defaultState,
dispatch: defaultDispatch,
});
@jamesseanwright
jamesseanwright / ryor-provider.tsx
Last active December 21, 2018 16:44
Roll Your Own Redux Provider
export const Provider: React.FC<ProviderProps> = ({
reducer,
children,
}) => {
const [state, dispatch] = React.useReducer(reducer, defaultState);
return (
<StateContext.Provider value={{
state,
dispatch,
@jamesseanwright
jamesseanwright / ryor-connect.tsx
Created December 21, 2018 17:00
Roll Your Own Redux Connect Impl
export const connect = <TStateProps, TDispatchProps, TOwnProps = {}>(
mapStateToProps?: (state: State, ownProps: TOwnProps) => TStateProps,
mapDispatchToProps?: (dispatch: AugmentedDispatch, ownProps: TOwnProps) => TDispatchProps,
) =>
(Component: React.ComponentType<TStateProps & TDispatchProps & TOwnProps>) => (
(props: TOwnProps) =>
<StateContext.Consumer>
{({ state, dispatch }) => (
<Component
{...props}
@jamesseanwright
jamesseanwright / redux-thunk.tsx
Created December 21, 2018 17:29
Redux Thunk Example
export const addRonSwansonQuote = () =>
(dispatch: React.Dispatch<Action>) => {
dispatch(setQuoteLoading());
return fetch('https://ron-swanson-quotes.herokuapp.com/v2/quotes')
.then(res => res.json())
.then(([quote]: string[]) =>
dispatch(addMessage(quote)),
)
.catch(() => dispatch(setQuoteError()));
@jamesseanwright
jamesseanwright / ryor-augmented-dispatch.tsx
Last active July 12, 2020 07:01
Augmenting useReducer's dispatch
export type Thunk = (dispatch: React.Dispatch<Action>, state: State) => void;
export type AugmentedDispatch = React.Dispatch<Thunk | Action>;
const augmentDispatch = (dispatch: React.Dispatch<Action>, state: State) =>
(input: Thunk | Action) =>
input instanceof Function ? input(dispatch, state) : dispatch(input);
export const Provider: React.FC<ProviderProps> = ({
reducer,
children,
@jamesseanwright
jamesseanwright / ryor-reducer.tsx
Created December 22, 2018 17:40
Example Reducer
const rootReducer: React.Reducer<State, Action> = (state, action) => {
if (isAddMessage(action)) {
const { message } = action.payload;
return {
...state,
isLoadingQuote: false,
hasQuoteError: false,
isFormValid: !!message,
messages: [
@jamesseanwright
jamesseanwright / ryor-action-creator.ts
Created December 22, 2018 17:52
Redux action creator example
export const addMessage = (message: string) => ({
type: ADD_MESSAGE,
payload: {
message,
},
});