Skip to content

Instantly share code, notes, and snippets.

View bebraw's full-sized avatar

Juho Vepsäläinen bebraw

View GitHub Profile
@goatslacker
goatslacker / alt-store-reducer.js
Created June 4, 2015 05:27
Writing a simple store as a reducer
import Alt from 'alt'
const alt = new Alt()
const CounterActions = alt.createActions({
inc: x => x,
dec: x => x,
});
const CounterStore = alt.createStore({
@johanobergman
johanobergman / 1. Info.md
Last active February 7, 2019 21:01
Laravel-style DI in JavaScript

Laravel-style DI in JavaScript

Honestly, coming from PHP, I really don't like the way dependencies are handled in JavaScript. Using require() or import either gives me a singleton object, or a class that I have to instantiate myself.

The DI container in Laravel is wonderful, and allows you to basically just ask for a dependency in a class constructor and it hands it to you. You can bind things to the container, but you can also resolve things without explicitly binding them, which I think is awesome.

@jquense
jquense / jsx-mode.js
Last active October 17, 2015 05:40
An attempt at a jsx CodeMirror mode
/*global CodeMirror */
function indexOf(string, pattern, from) {
if (typeof pattern === "string") {
var found = string.indexOf(pattern, from);
return found;
}
var m = pattern.exec(from ? string.slice(from) : string);
return m ? (m.index + from) : -1;
}
@goatslacker
goatslacker / transforms-with-alt.js
Last active August 29, 2015 14:20
Nuclear-style transforms for Alt stores
import Alt from './'
// the magic sauce
import { createTransform } from './utils/frp'
// decorator utilities
import { createStore, bind } from './utils/decorators'
const alt = new Alt()
@mjackson
mjackson / AutoBindingComponent.js
Created May 6, 2015 20:35
A React.Component subclass that mimics 0.12's auto-binding behavior
var React = require('react');
class AutoBindingComponent extends React.Component {
constructor(props) {
super(props);
for (var property in this) {
if (this.hasOwnProperty(property) && typeof this[property] === 'function') {
this[property] = this[property].bind(this);
}
@gaearon
gaearon / reduce-store-time-travel.js
Last active January 30, 2024 05:08
Time travelling concept with reducey stores and state atoms inspired by https://gist.github.com/threepointone/43f16389fd96561a8b0b#comment-1447275
/**
* Stores are just seed + reduce function.
* Notice they are plain objects and don't own the state.
*/
const countUpStore = {
seed: {
counter: 0
},
reduce(state, action) {
@jquense
jquense / 0. intro.md
Last active September 24, 2022 05:10
Alternative ways to define react Components

The 0.13.0 improvements to React Components are often framed as "es6 classes" but being able to use the new class syntax isn't really the big change. The main thing of note in 0.13 is that React Components are no longer special objects that need to be created using a specific method (createClass()). One of the benefits of this change is that you can use the es6 class syntax, but also tons of other patterns work as well!

Below are a few examples creating React components that all work as expected using a bunch of JS object creation patterns (https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch4.md#mixins). All of the examples are of stateful components, and so need to delegate to React.Component for setState(), but if you have stateless components each patterns tends to get even simpler. The one major caveat with react components is that you need to assign props and context to the component instance otherwise the component will be static. The reason is

@eldh
eldh / InViewWrapper.coffee
Created April 24, 2015 08:30
react-in-view
module.exports = React.createClass
displayName: 'InViewWrapper'
propsTypes:
onInView: React.PropTypes.func.isRequired
inViewOffset: React.PropTypes.number
getDefaultProps: ->
inViewOffset: 0
@gaearon
gaearon / Stateful.js
Created April 23, 2015 16:57
Stateful.js
import React, { Component } from 'react';
// Usage:
//
// @Stateful({
// initialize(props) {
// return {
// count: 0
// };
// },
@eldh
eldh / breakpoint-mixin.coffee
Last active August 29, 2015 14:19
React breakpoints mixin
Style = require '../styles/props'
_ = require 'lodash'
module.exports =
_setScreenWidth: ->
if @isMounted() then @setState {screenWidth: window.innerWidth}
getInitialState: -> screenWidth: window?.innerWidth or 1280
screenWidth: (minName, maxName) ->