Skip to content

Instantly share code, notes, and snippets.

View johnborges's full-sized avatar
Focusing

John Borges johnborges

Focusing
View GitHub Profile
@johnborges
johnborges / responsiveTables.css
Created October 23, 2019 17:58
Responsive HTML Tables
/* Default span styling - hidden on desktop */
table td span {
background: #eee;
color: dimgrey;
display: none;
font-size: 10px;
font-weight: bold;
padding: 5px;
position: absolute;
text-transform: uppercase;
@johnborges
johnborges / destructuring.js
Created October 14, 2019 17:17
Cool ways of destructuring in JS
// skip items
let [a , , b] = [1, 2, 3];
// quick swap variables
let a = 1;
let b = 3;
[a, b] = [b, a];
// assignment without declaration
@johnborges
johnborges / eval-js.js
Created October 7, 2019 18:14
Evaluating JavaScript using the import() statement
const js = `console.log('Hello everyone!');`;
const encodedJs = encodeURIComponent(js);
const dataUri = 'data:text/javascript;charset=utf-8,'
+ encodedJs;
import(dataUri);
// Output:
// 'Hello everyone!'
@johnborges
johnborges / react-component-commandments.md
Last active September 6, 2019 18:15
10 Component Commandments

10 React Component Commandments

  1. Document the Usage If you don't document how your component is supposed to be used, it's by definition useless.

  2. Allow for contextual semantics Allow your components to accept an as prop, which will consistently let you override what DOM element is being rendered.

function Grid({ as: Element, ...props }) {
 return 
@johnborges
johnborges / clean-jsx.js
Last active August 28, 2019 01:43
Logic-Less JSX. Keep the JSX of your components clean by putting data logic in variables.
const Animal = ({ id, name, legCount, isFriendly }) => {
const url = `url/animal/${id}`
const legCountStr = toString(legCount) || '?'
const friendliness = { true: 'Friendly', false: 'Unfriendly' }[isFriendly]
const hasNotEnoughData = legCount === undefined && isFriendly === undefined
return (
<li>
<a href={url}>{name}</a> - {legCountStr} legs
{friendliness && ` - ${friendliness}`}
@johnborges
johnborges / async-error.js
Created July 21, 2019 22:55
Async Await Error Handling
run().
catch(function handleError(err) {
err.message; // Oops!
}).
// Handle any errors in `handleError()`. If the error handler
// throws an error, kill the process.
catch(err => { process.nextTick(() => { throw err; }) });
async function run() {
await Promise.reject(new Error('Oops!'));