Jest Mock Any Property on Window Utility - with automatic cleanup.
// source: https://stackoverflow.com/a/59843241 | |
const usePrevious = (value, initialValue) => { | |
const ref = React.useRef(initialValue); | |
React.useEffect(() => { | |
ref.current = value; | |
}); | |
return ref.current; | |
}; |
class Grid extends React.Component { | |
_timeoutID; | |
constructor() { | |
this.state = { | |
isManagingFocus: false, | |
}; | |
} | |
All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.
Generally, all APIs that synchronously provide layout metrics will trigger forced reflow / layout. Read on for additional cases and details.
elem.offsetLeft
,elem.offsetTop
,elem.offsetWidth
,elem.offsetHeight
,elem.offsetParent
var express = require('express'); | |
var cookieParser = require('cookie-parser'); | |
var session = require('express-session'); | |
var flash = require('express-flash'); | |
var handlebars = require('express-handlebars') | |
var app = express(); | |
var sessionStore = new session.MemoryStore; | |
// View Engines |
// array utils | |
// ================================================================================================= | |
const combine = (...arrays) => [].concat(...arrays); | |
const compact = arr => arr.filter(Boolean); | |
const contains = (() => Array.prototype.includes | |
? (arr, value) => arr.includes(value) | |
: (arr, value) => arr.some(el => el === value) |
I recently began working with Node and MongoDB for a small personal project, largely just to learn the technologies. One thing that is fairly simple but that I found far from obvious and lacking in concrete examples was how to populate the part of my database that used referenced collections from the sample JSON data I was starting with. This post attempts to fill that gap using the following code snippets, which are heavily commented inline. You will notice I am using the awesome Mongoose library which makes working with MongoDB very easy.
http.createServer( app ).listen( app.get( 'port' ), function() {
mongoose.connect( 'mongodb://localhost/{YOUR_DB_NAME}' );
var db = mongoose.connection;