Skip to content

Instantly share code, notes, and snippets.

View skellock's full-sized avatar

Steve Kellock skellock

View GitHub Profile
@capaj
capaj / component.js
Last active June 22, 2016 12:40
react stateful function component
import React from 'react'
import {observer} from 'mobx-react'
import {observable} from 'mobx'
const state = observable({
value: 0
})
const Comp = (props) => {
return <div onClick={() => state.value++}>click to increase counter value: {state.value}</div>
@knowbody
knowbody / App.js
Last active September 11, 2023 09:31
Check internet connection in React Native app
// quick snippet to check the connection in your RN app
// dispatches an `setIsConnected` action every time the NetInfo changes (on/off network)
componentDidMount() {
const dispatchConnected = isConnected => this.props.dispatch(setIsConnected(isConnected));
NetInfo.isConnected.fetch().then().done(() => {
NetInfo.isConnected.addEventListener('change', dispatchConnected);
});
}
@yelouafi
yelouafi / Counter.jsx
Last active October 5, 2016 07:13
redux-saga with vanilla React
import React, { Component } from 'react'
import { runSaga, delay } from 'redux-saga'
import { take, put, call } from 'redux-saga/effects'
// helper functions
const bindFunc = (comp, method) => (...args) => method.apply(comp, args)
const bindSaga = (comp, method) => (...args) => runSaga(method.apply(comp, args), {})
export default class Counter extends React.Component {
@andymatuschak
andymatuschak / States-v3.md
Last active May 1, 2024 12:32
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

@littlepsylo
littlepsylo / index.js
Last active July 28, 2016 13:37
Using WebSocket in react-native with a socket.io server
const io = new WebSocket('ws://yourdomain:port/socket.io/?transport=websocket')
io.onclose = e => console.log('onclose', e.code, e.reason)
io.onerror = e => console.log('onerror', e.message)
io.onopen = () => console.log('connected !')
@mmazzarolo
mmazzarolo / ContainerExample1.js
Created August 3, 2016 07:34
Authentication duck example
// ACTIONCREATORS USAGE EXAMPLE 1
// When the action needed are all in a single duck (in this case authReducer)
import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { actionCreators } from '../reducers/authReducer'
const mapStateToProps = (state, ownProps) => ({
// blablabla
})
@mmazzarolo
mmazzarolo / redux_naming_example.js
Created September 6, 2016 21:44
Redux naming example
import { find, findIndex, pullAt } from 'lodash'
import { actionTypes as appActionTypes } from 'reducers/appReducer'
export const actionTypes = {
CREATE_MENU_ENTRY_REQUEST: 'MENU/CREATE_MENU_ENTRY_REQUEST',
CREATE_MENU_ENTRY_SUCCESS: 'MENU/CREATE_MENU_ENTRY_SUCCESS',
CREATE_MENU_ENTRY_FAILURE: 'MENU/CREATE_MENU_ENTRY_FAILURE',
UPDATE_MENU_ENTRY_REQUEST: 'MENU/UPDATE_MENU_ENTRY_REQUEST',
UPDATE_MENU_ENTRY_SUCCESS: 'MENU/UPDATE_MENU_ENTRY_SUCCESS',
UPDATE_MENU_ENTRY_FAILURE: 'MENU/UPDATE_MENU_ENTRY_FAILURE',
@Jeevuz
Jeevuz / isDeviceLocked.java
Created September 20, 2016 10:41
Check device is currently locked
/**
* Returns true if the device is locked or screen turned off (in case password not set)
*/
public static boolean isDeviceLocked(Context context) {
boolean isLocked = false;
// First we check the locked state
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean inKeyguardRestrictedInputMode = keyguardManager.inKeyguardRestrictedInputMode();
@mjackson
mjackson / resolvePromise.js
Last active September 9, 2018 08:23
An easy way to do async APIs in JavaScript that support both promises *and* callbacks!
// Here is a function that I use all the time when creating public
// async APIs in JavaScript:
const resolvePromise = (promise, callback) => {
if (callback)
promise.then(value => callback(null, value), callback)
return promise
}
// Sometimes I like to use callbacks, but other times a promise is
@mochja
mochja / main.c
Last active September 25, 2023 15:08
Yoga + OpenGL Example
#include <GLFW/glfw3.h>
#include <yoga/Yoga.h>
#include <stdlib.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())