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
| // Container Pattern: https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0 | |
| import Loading from './Loading' | |
| import RenderUser from './RenderUser' | |
| class User extends Component { | |
| state = { loading: true } | |
| render() { | |
| const { loading, user } = this.state | |
| return loading ? <Loading /> : <RenderUser user={user} /> |
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
| // Good | |
| const TableRowWrapper = ({ children }) => ( | |
| <tr> | |
| {children} | |
| </tr> | |
| ) | |
| // Bad | |
| class TableRowWrapper extends Component { | |
| render() { |
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 MyComponent = ({ className, ...others }) => ( | |
| <div className={className}> | |
| <MyOtherComponent {...others} /> | |
| </div> | |
| ) |
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 state = [] | |
| // Actions => tells the reducer which action to perform and provides the values | |
| const action = [ | |
| { type: 'ADD_TODO', text: 'do laundry', completed: false, id: 0 }, | |
| { type: 'ADD_TODO', text: 'walk the kids', completed: false, id: 1 }, | |
| { type: 'ADD_TODO', text: 'take the dogs to school', completed: false, id: 2 }, | |
| { type: 'ADD_TODO', text: 'kiss the plant', completed: false, id: 3 }, | |
| { type: 'ADD_TODO', text: 'water the wifey', completed: false, id: 4 }, | |
| { type: 'REMOVE_TODO', id: 0 }, |
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 express = require('express') | |
| const db = require('./data/helpers/tagDb') | |
| const server = express() | |
| server.use(express.json()) | |
| // Catches async/await errors/Promise rejections and passes it along to express middleware | |
| const catchErrors = fn => (req, res, next) => fn(req, res, next).catch(next) | |
| // Error Handler (displays full error details) | |
| const handleErrors = (err, req, res, next) => { |
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
| // 🔥 Node 7.6 has async/await! Here is a quick run down on how async/await works | |
| const axios = require('axios'); // promised based requests - like fetch() | |
| const getCoffee = () => new Promise(resolve => { | |
| setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee | |
| }); | |
| async go = () => { | |
| try { |
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 fruits = { | |
| apple: 'better', | |
| banana: 'good', | |
| peach: 'ok', | |
| pear: 'good' | |
| } | |
| Object | |
| .entries(fruits) | |
| .reduce((goodFruit, [fruit, quality]) => { |
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
| <html> | |
| <body> | |
| <div id="root"></div> | |
| </body> | |
| <script> | |
| const rootEl = document.getElementById('root') | |
| function createElement(type, props, ...children) { | |
| const element = document.createElement(type) | |
| Object.entries(props).forEach(([key, value]) => { |
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
| /* —— thunks | |
| * A nullary function (no arguments) that returns closured value(s). | |
| * Basically a token or wrapper that represents a specific value. | |
| */ | |
| const add = (x, y) => x + y | |
| const thunk = () => add(6, 3) | |
| thunk() // 8 |
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 mapValues(obj, fn) { | |
| return Object.keys(obj).reduce((result, key) => { | |
| result[key] = fn(obj[key], key); | |
| return result; | |
| }, {}); | |
| } | |
| function pick(obj, fn) { | |
| return Object.keys(obj).reduce((result, key) => { | |
| if (fn(obj[key])) { |