Check if Logged in – React Router App ES6

Every route has an onEnter hook which is called before the route transition happens. Handle the onEnter hook with a custom requireAuth function. <Route path=”/search” component={Search} onEnter={requireAuth} /> A sample requireAuth is shown below. If the user is authenticated, transition via next(). Else replace the pathname with /login and transition via next(). The login is … Read more

Is it possible to reset an ECMAScript 6 generator to its initial state?

If your intention is to some other scope, iterate over it, do some other stuff, then be able to iterate over it again later on in that same scope. Then the only thing you shouldn’t try doing is passing the iterator, instead pass the generator: var generator = function*() { yield 1; yield 2; yield … Read more

How to check if a variable is an ES6 class declaration?

If you want to ensure that the value is not only a function, but really a constructor function for a class, you can convert the function to a string and inspect its representation. The spec dictates the string representation of a class constructor. function isClass(v) { return typeof v === ‘function’ && /^\s*class\s+/.test(v.toString()); } Another … Read more

Better to export an object containing function, or just export multiple functions in ES6 (Is there a convention?)

Going forward, it’s probably better to export multiple functions, as tree shaking allows module bundlers to eliminate dead code based on whether it’s been imported or not. If you export one large object, it’s not always possible to use static analysis to tell which functions need to be kept and which can be safely discarded. … Read more

Get parent class name from child with ES6?

ES6 classes inherit from each other. So when instance.constructor refers to the Child, then you can use Object.getPrototypeOf(instance.constructor) to get the Parent, and then access .name: Object.getPrototypeOf(instance.constructor).name // == “Parent” Of course, full ES6 compliance and non-minified code are necessary for this to work. You should never rely on function names in code.