Skip to content

Instantly share code, notes, and snippets.

@dceddia
dceddia / karma.conf.js
Created December 30, 2015 02:33
Basic Angular Karma config file
// Karma configuration
// Generated on Tue Dec 29 2015 21:08:31 GMT-0500 (EST)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
@dceddia
dceddia / users.js
Last active October 10, 2017 23:07
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
// Comment out this line:
//res.send('respond with a resource');
// And insert something like this instead:
res.json([{
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:3001"
import React, { Component } from 'react';
import './App.css';
class App extends Component {
state = {users: []}
componentDidMount() {
fetch('/users')
.then(res => res.json())
.then(users => this.setState({ users }));
@dceddia
dceddia / App.js
Created October 20, 2017 15:32
Fetching users from Express, while also using React Router
import React, { Component } from 'react';
import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom';
import './App.css';
class UsersView extends Component {
state = {users: []}
import React from 'react';
class Counter extends React.Component {
state = { count: 0 }
increment = () => {
this.setState({
count: this.state.count + 1
});
}
import React from 'react';
import { render } from 'react-dom';
import Counter from './Counter';
const App = () => (
<div>
<Counter />
</div>
);
@dceddia
dceddia / Counter.js
Created November 17, 2017 04:23
With state removed
import React from 'react';
class Counter extends React.Component {
increment = () => {
// fill in later
}
decrement = () => {
// fill in later
}
function reducer(state = initialState, action) {
switch(action.type) {
case 'INCREMENT':
return {
count: state.count + 1
};
case 'DECREMENT':
return {
count: state.count - 1
};
function brokenReducer(state = initialState, action) {
switch(action.type) {
case 'INCREMENT':
// NO! BAD: this is changing state!
state.count++;
return state;
case 'DECREMENT':
// NO! BAD: this is changing state too!
state.count--;