Skip to content

Instantly share code, notes, and snippets.

@igrek8
igrek8 / App.jsx
Created May 6, 2019 15:14
Prevent useCallback rerendering children components
import React, { useState, useRef, useCallback } from "react";
import Button from "./Button";
function App() {
const [state, setState] = useState("");
const ref = useRef(state);
const handleChange = useCallback(event => {
ref.current = event.target.value;
setState(event.target.value);
}, []);
document.body.style.paddingBottom = '250px';
const div = document.createElement('div');
Object.assign(div.style, {
position: 'fixed',
bottom: 0,
left: 0,
right: 0,
height: '250px',
opacity: 0.90,
color: 'white',
@igrek8
igrek8 / parse-redux-form-field.js
Created June 6, 2018 07:36
Parse complex string redux-form
const escapeRegex = /([^\\])\./g;
export const normalizeToken = index => (str, prev) => {
if (
typeof str === 'string' ||
typeof str === 'number' ||
typeof str === 'boolean') {
const groups = prev.toString()
.replace(escapeRegex, '$1¬')
@igrek8
igrek8 / loader.css
Last active March 16, 2018 08:19
The simplest inline-block loader with inherit color
.loader {
color: inherit;
display: inline-block;
cursor: wait;
}
.loader > span {
animation-name: blink;
animation-duration: 1.4s;
animation-iteration-count: infinite;
@igrek8
igrek8 / recursive-redux-form.jsx
Created February 21, 2018 11:08
Recusive props in redux-form, Recursive render
/* eslint-disable react/prefer-stateless-function */
import React from 'react';
import { reduxForm, FieldArray, Field, FormSection } from 'redux-form';
import { TextField } from 'redux-form-material-ui';
const getDepth = (name) => {
if (name) {
const match = name.match(/children\[\d\]/g);
if (match) {
@igrek8
igrek8 / form.jsx
Created February 9, 2018 07:36
Simple implementation of AutoComplete & Select in Material UI with Redux Form
import React from 'react';
import { Field, reduxForm } from 'redux-form';
import { AutoComplete, FlatButton } from 'material-ui';
// Component
const mapError = (
{
meta: { touched, error, warning } = {},
input,
@igrek8
igrek8 / state-object.ts
Last active January 8, 2017 14:22
simple immutable state object with reflection
import * as deepMerge from 'deepmerge';
import * as deepFreeze from 'deep-freeze';
function sealed(constructor: Function) {
Object.seal(constructor);
Object.seal(constructor.prototype);
}
@sealed
export class StateObject<T> {
@igrek8
igrek8 / event-emittable.ts
Created January 6, 2017 08:59
typescript class mixins
import { Mixin } from './mixin';
import { EventEmitter } from 'events';
// Define .h
export declare class EventEmittable {
protected events: EventEmitter;
}
// Implement .cpp
export const EventEmittableMixin = (mixin: Mixin) => class extends mixin {
@igrek8
igrek8 / bitwise.ts
Last active December 24, 2016 11:01
nodejs simple class for bitwise ops
export class Bitwise {
private _i: number;
constructor(value: number) {
this._i = value;
}
test(mask = 0x01) {
return (mask ^ this._i) === 0;
@igrek8
igrek8 / buffer-offset.ts
Last active December 23, 2016 19:42
nodejs simple offset helper
export class SmartCursor {
private _cur: number;
private _memorized: number;
constructor(i: number = 0) {
this._cur = i;
this._memorized = i;
}