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);
const Ford = {
seats: 4,
passengers: [{ name: 'Bobby', height: 175 }, { name: 'Shirley', height: 150 }],
beep () {
console.log('Beep beep')
}
}
const FordCar = implement(Car)(Ford)
describe('getCar', () => {
it('should implement the Vehicle Interface', done => {
const someCar = CarService.getCar()
// Ensure someCar implements Vehicle Interface
implement(Vehicle)(someCar)
done()
})
})
@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 / update-state.js
Last active December 16, 2018 22:02
Updating our state
export class StateProvider extends Component {
static defaultProps = {
state: {}
};
state = this.props.state;
render () {
return (
<Provider value={{ state: this.state, setState: this.setState.bind(this) }}>
@Jahans3
Jahans3 / dispatching-actions.js
Last active December 10, 2018 18:24
Dispatching actions
export default function SomeCount () {
return (
<StateConsumer>
{({ state, dispatch }) => (
<>
<p>
Count: {state.count}
</p>
<button onClick={() => dispatch(addOne())}>
+ 1
@Jahans3
Jahans3 / consume-update.js
Last active December 10, 2018 17:41
Updating and consuming our state
export default function SomeCount () {
return (
<StateConsumer>
{({ state, setState }) => (
<>
<p>
Count: {state.count}
</p>
<button onClick={() => setState({ count: state.count + 1 })}>
+ 1
@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;
}