Skip to content

Instantly share code, notes, and snippets.

@threepointone
threepointone / deep-string.js
Last active March 13, 2019 20:09
deep update of text in a react component
import React from 'react'
import { render } from 'react-dom'
// with fiber, we'll be able to write components that update text deep
// inside another string without wrapper dom, or rerendering the whole component
// before
class Lorem extends React.Component {
state = {
str: ''
@threepointone
threepointone / eric.js
Last active September 16, 2021 11:22
<div className={'blue '+ css({ '&&': { color: 'red'}})}>
'&&' boosts specificity. add as many '&&&'s as you want
</div>,
<div className={css({ '& #meEle': { color: 'red' }})}>
<div id='myEle'>
targeting by id
</div>
</div>
let rule = css({
'& .Resizer': {
// ...
},
'& .SplitPane': {
// ...
}
'& .Pane': {
// ...
}
@threepointone
threepointone / example.js
Last active August 31, 2017 02:33
glamor for react-native?
import { View, Text } from 'react-native'
import { css } from 'glamor-native'
export App = () => {
<View style={css({ color: 'red' })}>
<Text>Hello world!</Text>
</View>
}
@threepointone
threepointone / ensure.js
Last active January 13, 2022 10:10
fix for ssr + dynamic imports with webpack
function ensure(moduleId, fn, done){
if(__webpack_modules__[moduleId]) { //eslint-disable-line no-undef
return done(undefined, __webpack_require__(moduleId)) //eslint-disable-line no-undef
}
fn().then(Module => done(undefined, Module), done)
}
// usage
ensure(require.resolveWeak('./x.js'),
@threepointone
threepointone / alternative.md
Last active July 31, 2022 17:46
list of things that don't do what they say they do

(also know as lies and/or alternative facts)

js

  • setImmediate - doesn't set anything immediately, waits for a tick before executing
  • setTimeout(fn, n) - never sets the timeout to exactly n
  • Math.random() - computers cannot generate random numbers
  • Promise - is a lie when rejected
  • Array.reduce - accumulates, does not reduce (via @sbmadhav)
@threepointone
threepointone / deferred.js
Created December 29, 2016 14:38
deferred render with react fiber
/* global React, ReactDOMFiber */
/* @jsx deferElement */
const { render, unstable_deferredUpdates } = ReactDOMFiber
class Deferred extends React.Component {
state = { next: false }
componentDidMount() {
unstable_deferredUpdates(() =>
this.setState(state => ({ next: true })))
}
@threepointone
threepointone / bad-promises.js
Created December 25, 2016 14:03
for ingvar
// consider the following code
// a.js
class A extends Component {
state = { B : undefined }
componentWillMount(){
import('./b.js').then(B =>
this.setState({ B }))
}
@threepointone
threepointone / gen-ric.js
Created December 23, 2016 04:59
generators + requestIdleCallback
// this is a generic runner for iterators
// for every yield, it checks to see if any time is remaining
// on the idle loop, and executes more work if so.
// else, it queues up for the next idle period
function go(it, callback){
requestIdleCallback(deadline => {
let val = it.next()
while(!val.done){
if(deadline.timeRemaining() <= 0){
@threepointone
threepointone / infinite.js
Created December 20, 2016 08:44
infinite scrolling pattern with react fiber (featuring intersection observers)
// inifinite scrolling of content without extra wrappers
const { render, findDOMNode } = ReactDOMFiber
class App extends React.Component {
render() {
// wrap the root element with an Intersection Observer, exposing .observe for children
return <Intersection>
<div style={{ height: 200, overflow: 'auto' }}>
<Page offset={0} count={10} />
</div>