Skip to content

Instantly share code, notes, and snippets.

View kishanio's full-sized avatar
🐈
C for Cat.

Kishan Thobhani kishanio

🐈
C for Cat.
  • Indie Solo Dev.
  • Tropical West Coast of India (Mostly)
View GitHub Profile
@kishanio
kishanio / dispatch_middlewares.js
Created May 11, 2017 10:52
Redux : Apply Middlewares to Dispatch
// Dependency : http://redux.js.org/
import { createStore, applyMiddleware } from 'redux';
import todoApp from './reducers';
// both promise and logger are middleware fucntion. See implementation here, https://egghead.io/lessons/javascript-redux-the-middleware-chain
import promise from 'redux-promise';
import logger from 'redux-logger';
const configureStore = () => {
// creating array of middleware
@kishanio
kishanio / configure_store_middleware.js
Last active May 11, 2017 10:38
JS Functional Programming : Currying Function 2
// Dependency : http://redux.js.org/
// Dependency : https://egghead.io/lessons/javascript-redux-the-middleware-chain
// following store resolves promises before by injecting middlewares to store dispatch.
const promise = (store) => (next) => (action) => {
if ( typeof action.then === 'function' ) {
return action.then(next);
}
return next(action);
};
@kishanio
kishanio / chrome_console.js
Last active May 7, 2017 18:11
Chrome : Console
// group
const testConsole = { "abc" : "abc", "def" : "def", "jkl" : "jkl" };
console.group("Group Console");
console.log(testConsole);
console.groupEnd("Group Console");
// colours
console.log("%c test", "color: red");
@kishanio
kishanio / mapdispatchprops_shorthand.js
Created May 5, 2017 13:48
Redux : Map Dispatch Props Shorthand
const mapDispatchToProps = (dispatch) => ({
onTodoClick(id) {
dispatch(toggleTodo(id));
},
});
const VisibleTodoList = withRouter( connect(
mapStateToProps,
mapDispatchToProps
)(TodoList) );
@kishanio
kishanio / currying_example.js
Created May 4, 2017 06:54
JS Functional Programming : Currying Function
// Reference : https://www.sitepoint.com/currying-in-functional-javascript/
var greetCurried = function(greeting) {
return function(name) {
console.log(greeting + ", " + name);
};
};
var greetHello = greetCurried("Hello");
@kishanio
kishanio / connect_example.js
Last active May 4, 2017 06:43
ReactRedux : Generating Container Components
// Dependency : https://github.com/reactjs/react-redux
// Writing a container to pass todos & onTodoClick as Props to child.
// We needed this container in order to subscribe and unsubscribe to state.
class VisibleTodoList extends Component {
componentDidMount() {
const { store } = this.context;
this.unsubscribe = store.subscribe( () => this.forceUpdate() );
}
@kishanio
kishanio / filter_example.js
Created May 3, 2017 17:15
ES5 : Array Filter
var someArr = [10, 5, 3, 55];
console.log( someArr.filter( e => e > 5 )); // [10,55]
// Dependecy : https://facebook.github.io/react/
const { Component } = React;
class App extends Component {
constructor() {
super();
this.state = { a: "",b : ""};
@kishanio
kishanio / arrow_function_example.js
Last active May 4, 2017 10:59
ES6 : Arrow Function
// normal function defination
const someFunction = ( someArg, someArgDef = "Default Arg" ) => {
return someArg + " " + someArgDef;
}
console.log( someFunction( "Some Argument" ) );
// single line single argument short hand
const arr = ["a", "b", "c"];
const newArr = arr.map(a=>a);
@kishanio
kishanio / manual_combine_reducers.js
Last active July 1, 2018 08:34
Redux : Manually implementing Combine Reducer
// Reference : https://egghead.io/lessons/javascript-redux-implementing-combinereducers-from-scratch
const combineReducers = (reducers) => {
return ( state = {}, action) => {
const keys = Object.keys(reducers);
const arr = keys.reduce((accumulator, key) => {
// since accumulator is created inside reducer function we don't have to worry about if it's mutable or not.
// reducer still stays as a pure function.
accumulator[key] = reducers[key](state[key],action);
return accumulator;