Skip to content

Instantly share code, notes, and snippets.

View dennisja's full-sized avatar
🕊️
I have learned that I have a lot to learn

dj dennisja

🕊️
I have learned that I have a lot to learn
View GitHub Profile
o_cache = {}
def fibonacci(num):
if num == 0:
return num
if num == 1:
return num
if num in o_cache:
cache = [0, 1]
def fibonacci(num):
if num == 0:
return num
if num == 1:
return num
@dennisja
dennisja / controlled-todo-add-form.js
Last active November 2, 2018 12:49
A simple form to show lessons learned about JS when building a simple controlled form in react
// We make use of ES6 imports
import React, { Component, Fragment } from "react";
// we make use of ES6 classes and inheritance
class TodoForm extends Component {
// we make use of class variables to initialize state
state = {
todoName: ""
};
@dennisja
dennisja / controlled-todo-add-form-no-class-variables.js
Created October 21, 2018 18:44
A simple form to show lessons learned about JS when building a simple controlled form in react without using class properties
import React, { Component, Fragment } from "react";
class TodoForm extends Component {
constructor(props) {
super(props);
this.state = {
todoName: ""
};
this.handleInputChange = this.handleInputChange.bind(this);
// assuming you are fetching data from a server returning a data structure shown below
const todos = [
{
id: 1,
title: "Go Home",
completed: false
},
{
id: 2,
title: "Say Hi",
// assuming you are fetching data from a server returning a data structure shown below
const todos = [
{
id: 1,
title: "Go Home",
completed: false
},
{
id: 2,
title: "Say Hi",
const Todo = ({ title, completed }) => (
<div>
{title}
{completed && <span> Well Done</span>}
{/* We use short circuting to conditionally determine what to render*/}
</div>
);
import React from "react";
// We use ES6 classed and inheritence
class Breweries extends React.Component {
constructor() {
// we use the constructor and call the constructor of the class we are in heriting from
super();
// we initialize state in the constructor
this.state = {
breweries: [],
componentDidMount() {
fetch("https://api.openbrewerydb.org/breweries")
.then(function(respose){return respose.json()})
.then(function(breweries){
this.setState({ breweries, loading: false });
}.bind(this))
.catch(function(error){
this.setState({ error, loading: false });
}.bind(this));
}
componentDidMount() {
// store the current value of this in a variable
const $this = this;
fetch("https://api.openbrewerydb.org/breweries")
.then(function(respose){return respose.json()})
.then(function(breweries){
// use the variable to setState
$this.setState({ breweries, loading: false });
})
.catch(function(error){