This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Ford = { | |
| seats: 4, | |
| passengers: [{ name: 'Bobby', height: 175 }, { name: 'Shirley', height: 150 }], | |
| beep () { | |
| console.log('Beep beep') | |
| } | |
| } | |
| const FordCar = implement(Car)(Ford) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe('getCar', () => { | |
| it('should implement the Vehicle Interface', done => { | |
| const someCar = CarService.getCar() | |
| // Ensure someCar implements Vehicle Interface | |
| implement(Vehicle)(someCar) | |
| done() | |
| }) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class StateProvider extends React.PureComponent { | |
| static defaultProps = { | |
| state: {}, | |
| reducers: [], | |
| middleware: [] | |
| }; | |
| state = this.props.state; | |
| _dispatch = action => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class StateProvider extends Component { | |
| static defaultProps = { | |
| state: {} | |
| }; | |
| state = this.props.state; | |
| render () { | |
| return ( | |
| <Provider value={{ state: this.state, setState: this.setState.bind(this) }}> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default function SomeCount () { | |
| return ( | |
| <StateConsumer> | |
| {({ state, dispatch }) => ( | |
| <> | |
| <p> | |
| Count: {state.count} | |
| </p> | |
| <button onClick={() => dispatch(addOne())}> | |
| + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default function SomeCount () { | |
| return ( | |
| <StateConsumer> | |
| {({ state, setState }) => ( | |
| <> | |
| <p> | |
| Count: {state.count} | |
| </p> | |
| <button onClick={() => setState({ count: state.count + 1 })}> | |
| + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ConnectState extends React.Component { | |
| state = {}; | |
| static getDerivedStateFromProps ({ state, mapState = s => s }) { | |
| return mapState(state); | |
| } | |
| shouldComponentUpdate (nextProps, nextState) { | |
| return shallowCompare(this.state, nextState); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
NewerOlder