Skip to content

Instantly share code, notes, and snippets.

View Jahans3's full-sized avatar
:shipit:

Josh J Jahans3

:shipit:
View GitHub Profile
@Jahans3
Jahans3 / App.tsx
Last active November 9, 2021 15:44
Fetching example hooks/classes
import React, { useState, useEffect, Component } from 'react';
import { UserAccount } from './typings';
// Example using hooks
export default function App(): React.ReactElement {
const [users, setUsers] = useState<UserAccount[]>([]);
useEffect(() => {
mockApi.fetchUsers()
.then(setUsers);
return (
<StateConsumer mapState={state => ({ greeting: state.greeting })} mapDispatch={dispatch => dispatch}>
// ...
@Jahans3
Jahans3 / final-connect-state.js
Last active December 6, 2018 22:13
Final ConnectState
class ConnectState extends React.Component {
state = {};
static getDerivedStateFromProps ({ state, mapState = s => s }) {
return mapState(state);
}
shouldComponentUpdate (nextProps, nextState) {
return shallowCompare(this.state, nextState);
}
@Jahans3
Jahans3 / shallow-compare.js
Last active December 5, 2018 17:55
Shallow compare
function shallowCompare (state, nextState) {
if ((typeof state !== 'object' || state === null || typeof nextState !== 'object' || nextState === null)) return false;
return Object.entries(nextState).reduce((shouldUpdate, [key, value]) => state[key] !== value ? true : shouldUpdate, false);
}
@Jahans3
Jahans3 / connect-state.js
Last active December 5, 2018 15:41
State consumer
class ConnectState extends React.Component {
state = this.props.mapState(this.props.state);
static getDerivedStateFromProps (nextProps, nextState) {}
shouldComponentUpdate (nextProps) {
if (!Object.keys(this.state).length) {
this.setState(this.props.mapDispatch(this.props.state));
return true;
}
@Jahans3
Jahans3 / dispatch-with-thunk.js
Last active February 23, 2019 14:18
Dispatch with async support
export class StateProvider extends React.PureComponent {
static defaultProps = {
state: {},
reducers: [],
middleware: []
};
state = this.props.state;
_dispatch = action => {
@Jahans3
Jahans3 / login-async.js
Created December 1, 2018 19:47
Login async
const logIn = (email, password) => async dispatch => {
dispatch({ type: 'LOG_IN_REQUEST' });
try {
const user = api.authenticateUser(email, password);
dispatch({ type: 'LOG_IN_SUCCESS', payload: user });
catch (error) {
dispatch({ type: 'LOG_IN_ERROR', payload: error });
}
};
@Jahans3
Jahans3 / final-provider-usehooks.js
Created December 1, 2018 01:59
Final provider with hooks
export function StateProvider ({ state: initialState, reducers, middleware, children }) {
return (
<StateContext.Provider value={useStateProvider({ initialState, reducers, middleware })}>
{children}
</StateContext.Provider>
);
}
@Jahans3
Jahans3 / use-state-provider.js
Last active December 1, 2018 20:11
Use state provider
export function useStateProvider ({ initialState, reducers, middleware = [] }) {
const [state, _dispatch] = useReducer((state, action) => {
return reducers.reduce((state, reducer) => reducer(state, action) || state, state);
}, initialState);
function dispatch (action) {
if (typeof action === 'function') {
return action(dispatch, state);
}
@Jahans3
Jahans3 / dispatch-using-hooks.js
Created December 1, 2018 01:41
Dispatch using hooks
export default function SomeCount () {
const [state, dispatch] = useStore();
return (
<>
<p>
Count: {state.count}
</p>
<button onClick={() => dispatch(addOne())}>
+ 1
</button>