Skip to content

Instantly share code, notes, and snippets.

View treyhuffine's full-sized avatar

Trey Huffine treyhuffine

View GitHub Profile
@treyhuffine
treyhuffine / functional-component.jsx
Last active February 9, 2019 21:37
A TypeScript functional component
import * as React from 'react';
interface IProps {
name?: string;
}
const Header: React.FC<IProps> = (props: IProps) => (
<h1>Hello, {props.name}! Welcome to React and TypeScript.</h1>
);
componentWillUnmount() {
window.removeEventListener('resize', this.resizeEventHandler);
}
shouldComponentUpdate(nextProps, nextState) {
return this.props.clicks !== nextProps.clicks;
}
componentWillReceiveProps(nextProps) {
if (this.props.id !== nextProps.id) {
this.setState({
feedContent: []
});
}
}
componetDidMount() {
fetch('https://gitconnected.com')
.then((res) => {
this.setState({
user: res.user
});
});
}
class Clicker extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.state = {
clicks: 0
};
}
handleClick() {
this.setState({
import React, { Component } from 'react'
import debounce from 'lodash/debounce';
// Create fake projects instead of using a database
let fakeProjects = [];
for (let i = 0; i < 1000000000; i++) {
fakeProjects.push({
id: id,
name: `project ${i}`,
featured: i % 2 === 0,
@treyhuffine
treyhuffine / PromiseSimpleExample.js
Last active May 6, 2025 23:51
An example using a simple promise implementation
class PromiseSimple {
constructor(executionFunction) {
this.promiseChain = [];
this.handleError = () => {};
this.onResolve = this.onResolve.bind(this);
this.onReject = this.onReject.bind(this);
executionFunction(this.onResolve, this.onReject);
}
@treyhuffine
treyhuffine / PromiseSimple.js
Last active November 2, 2022 13:21
A simple JavaScript Promise implementation for education purposes
class PromiseSimple {
constructor(executionFunction) {
this.promiseChain = [];
this.handleError = () => {};
this.onResolve = this.onResolve.bind(this);
this.onReject = this.onReject.bind(this);
executionFunction(this.onResolve, this.onReject);
}
// Originally inspired by David Walsh (https://davidwalsh.name/javascript-debounce-function)
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// `wait` milliseconds.
const debounce = (func, wait) => {
let timeout;
return function executedFunction(...args) {
const later = () => {