Skip to content

Instantly share code, notes, and snippets.

@ihgrant
ihgrant / example.js
Last active January 25, 2016 17:39
react controller outside react component
const Controller = (function () {
let currentRows = [];
const addRow = function (newRow) {
// add a new row to our local variable
render();
};
const render = function () {
ReactDOM.render(<Form onSubmit={addRow}/>, document.getElementById('myform'));
ReactDOM.render(<Rows rows={currentRows}/>, document.getElementById('myrows'));
};
var Question = React.createClass({
onChangeTitle: function (e) {
this.props.qTitleCallback(this.props.ordering, e);
},
onChangeType: function (e) {
this.props.qTypeCallback(this.props.ordering, e);
},
render: function() {
return (
<div>
{
"ecmaFeatures": {
"jsx": true,
"modules": true
},
"env": {
"browser": true,
"es6": true,
"node": true,
"jquery": false
@ihgrant
ihgrant / state.js
Last active May 3, 2016 21:49 — forked from sethdorris/state.js
Passing WebSocket Instance
const server = new WebSocket("ws://localhost:3000");
export default const server = {
sendmessage: () => {
return server.onmessage = () => {}
}
}
@ihgrant
ihgrant / create-connection.js
Created May 23, 2016 21:17
using mssql node library with promise support
const sql = require('mssql');
const Promise = require('bluebird');
Promise.promisifyAll([
sql.Connection,
]);
const createConnection = connectionOptions => {
const connection = new sql.Connection(connectionOptions);
return connection.connectAsync().then(() => {
@ihgrant
ihgrant / index.js
Last active November 11, 2016 01:01
simple application state singleton
import App from './App';
import './index.css';
import {render} from './statemachine';
render(
App,
{},
document.getElementById('root')
);
@ihgrant
ihgrant / model_react.jsx
Last active August 2, 2016 19:57 — forked from SuperManEver/model_react.jsx
adding model to react app
const React = require('react');
const ReactDOM = require('react-dom');
const TodoInput = require('./todo_input.js');
const TodoList = require('./todo_list.js');
var tasks = [
{ text : 'buy milk' },
{ text : 'do exercises' }
];
export function tryparseJson(jsonstring) {
try {
return JSON.parse(jsonstring);
} catch (e) {
return false;
}
}
@ihgrant
ihgrant / example.jsx
Last active September 8, 2016 15:43
ReactJS: Avoid binding to event handlers with composition
class Example extends React.Component {
onClick(e, id) {
...
}
render() {
return <ul>{this.props.items.map((el, i) => <li onClick={this.onClick.bind(this, el.id)}>{el.description}</li>)</ul>
}
}
/// OR........
@ihgrant
ihgrant / readme.md
Created December 1, 2016 17:47
file upload with dropzone

naïve approach

  • rely on files property being set
  • fails because while clicking on the input and selecting files will set the property correctly, dragging and dropping does not actually interact with the input and set the property. The property cannot be set manually (react-dropzone/react-dropzone#131) using javascript for security reasons.
  • thus when the form is submitted the file input has no files property and nothing is uploaded.

let's use javascript, then!

  • submit the form using fetch, create a FormData from the form and manually set the files field using state set using the onDrop callback from Dropzone
  • only set the files field if it's not set (because the user could have clicked and chosen files instead of dragging and dropping)
  • FormData has a great API for inspecting its keys and values! I'll use FormData#getAll() and see if the files are populated
  • works great in chrome and ff!