Skip to content

Instantly share code, notes, and snippets.

View dawsbot's full-sized avatar
💭
Bringing web3 to emerging markets - web3perf

Dawson Botsford dawsbot

💭
Bringing web3 to emerging markets - web3perf
View GitHub Profile
@rsms
rsms / optional.ts
Created July 28, 2017 16:11
Make a TypeScript interface completely optional
// Optional declares a type of keyed type T where all keys are optional.
// This allows
type Optional<T> = { [P in keyof T]? :T[P] }
interface Foo {
x :number
y :number
}
type OptionalFoo = Optional<Foo>
@bszwej
bszwej / echo.js
Last active April 16, 2024 14:31
Pure Node.js echo server, that logs all incoming http requests (method, path, headers, body).
const http = require('http');
const server = http.createServer();
server.on('request', (request, response) => {
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
@radiovisual
radiovisual / .eslintrc
Last active October 30, 2021 11:55
React Native AirBnB ESLint Config
{
"parser": "babel-eslint",
"plugins": [
"react",
"react-native"
],
"parserOptions": {
"ecmaFeatures": {
"jsx": true,
"modules": true
@marcbachmann
marcbachmann / .hyperterm.js
Last active January 10, 2024 06:58
hyperterm config
module.exports = {
config: {
// default font size in pixels for all tabs
fontSize: 12.5,
// font family with optional fallbacks
fontFamily: '"Meslo LG S for Powerline", Menlo, "DejaVu Sans Mono", "Lucida Console", monospace',
// terminal cursor background color (hex)
cursorColor: 'rgba(255,255,255,.4)',

Managing State in React

After digging more into React I get a better idea on how React states work. It looks like React still have a bit of magic happening behind the scene to make states work.

// 3 Elements mapping to the same real DOM
var element1 = <MyElement awesome=true>My Awesome Content</MyElement>

var element2 = <MyElement awesome=false>My Boring Content</MyElement>
@markerikson
markerikson / appEntryPoint.js
Last active August 1, 2022 07:41
Webpack React/Redux Hot Module Reloading (HMR) example
import React from "react";
import ReactDOM from "react-dom";
import configureStore from "./store/configureStore";
const store = configureStore();
const rootEl = document.getElementById("root");
@markerikson
markerikson / react-controlled-inputs.md
Last active June 15, 2021 12:50
React "controlled" vs "uncontrolled" inputs explanation

[12:03 AM] acemarke: "controlled" and "uncontrolled" inputs
[12:04 AM] acemarke: if I have a plain, normal HTML page, and I put <input id="myTextbox" type="text" /> in my page(edited)
[12:04 AM] acemarke: and I start typing into that textbox
[12:04 AM] acemarke: it remembers what I've typed. The browser stores the current value for that input
[12:05 AM] acemarke: and then sometime later, I can get the actual element, say, const input = document.getElementById("myTextbox"), and I can ask it for its value: const currentText = input.value;
[12:05 AM] acemarke: good so far?
[12:08 AM] acemarke: I'll keep going, and let me know if you have questions
[12:08 AM] lozio: ok, actually I'm reading
[12:09 AM] lozio: good
[12:09 AM] acemarke: so, a normal HTML input field effectively stores its own value at all times, and you can get the element and ask for its value

@pirate
pirate / parseURLParameters.js
Last active December 15, 2023 07:17
Parse URL query parameters in ES6
function getUrlParams(search) {
const hashes = search.slice(search.indexOf('?') + 1).split('&')
const params = {}
hashes.map(hash => {
const [key, val] = hash.split('=')
params[key] = decodeURIComponent(val)
})
return params
}
@dawsbot
dawsbot / gitBackup.sh
Created January 29, 2016 09:07
Always have your code in two git servers by backing up with Bitbucket
#Enter this as one line
git remote set-url — add — push origin git@bitbucket.org:dawsonbotsford/vimrcbuilder.git
#Re-add the original URL as well. Again do this with one line
git remote set-url — add — push origin git@github.com:dawsonbotsford/vimrcBuilder.git
#Verify everything worked
git remote -v
@dagingaa
dagingaa / prototype-to-class.js
Last active December 23, 2022 11:38
Codemod to transform well-written function prototype style classes into the new ES2015 syntax. Requires jscodeshift.
module.exports = (file, api, options) => {
const j = api.jscodeshift;
const root = j(file.source);
// We have to add "use strict" for node to play nice
// Taken from https://github.com/cpojer/js-codemod/blob/master/transforms/use-strict.js
const hasStrictMode = body =>
body.some(
statement => j.match(statement, {
type: 'ExpressionStatement',