Skip to content
Advertisement

Tag: ecmascript-6

React Bootstrap Components not displaying

I’m trying to test out the carousel but when I load the page it is blank. I installed react bootstrap with npm. The carousal code is from https://react-bootstrap.github.io/components.html#media-content. I looked at my web console and I don’t have any warnings or errors either. carousal.js index.js index.html Answer Found the solution. It is not Bootstrap related but rather how I was

How does `Array.from({length: 5}, (v, i) => i)` work?

I may be missing something obvious here but could someone breakdown step by step why Array.from({length: 5}, (v, i) => i) returns [0, 1, 2, 3, 4]? https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from I didn’t understand in detail why this works Answer When Javascript checks if a method can be called, it uses duck-typing. That means when you want to call a method foo from

Use reselect selector with parameters

How do I pass additional parameters to combined selectors? I am trying to • Get data • Filter data • Add custom value to my data set / group data by myValue console.log(myValue) returns undefined Answer Updated: 16 February 2022 New Solution from Reselect 4.1: See detail Updated: 6 March 2021 Solution from Reselect: See detail Old: This is my

Serializing an ES6 class object as JSON

I’d like to serialize myClass object to json. One easy way I can think of is, since every member is actually javascript object (array, etc..) I guess I can maintain a variable to hold the member variables. this.prop.foo = this.foo and so on. I expected to find a toJSON/fromJSON library for class objects since I used them with other languages

Custom Error Object with Apollo Server

I’m trying to use a custom error with apollo-server and it seems that my custom error has a property (code) that isn’t available from within formatError. I have a simple error handler works something like this: I’m having trouble because when I log error.code from within formatError it’s not available. How can I propagate custom properties (like code) of error

Why do both Promise’s then & catch callbacks get called?

I have the following code and when it’s executed, it returns both “rejected” and “success”: Could anyone explain why success is logged? Answer The then callback gets called because the catch callback is before it, not after. The rejection has already been handled by catch. If you change the the order (i.e. (promise.then(…).catch(…))), the then callback won’t be executed. MDN

Re-export default in ES 6 modules

In ES6, is it possible to shorten the following code. I have an App.js file and an index.js. index.js Something like this index.js Answer If you use proposal-export-default-from Babel plugin (which is a part of stage-1 preset), you’ll be able to re-export default using the following code: For more information see the ECMAScript proposal. Another way (without this plugin) is:

Arrow function without curly braces

I’m new to both ES6 and React and I keep seeing arrow functions. Why is it that some arrow functions use curly braces after the fat arrow and some use parentheses? For example: vs. Answer The parenthesis are returning a single value, the curly braces are executing multiple lines of code. Your example looks confusing because it’s using JSX which

Concatenating variables and strings in React

Is there a way to incorporate React’s curly brace notation and an href tag? Say we have the following value in the state: and the following HTML attributes on a tag: Is there a way I can add the id state to the HTML attribute to get something like this: Which will yield: Answer You’re almost correct, just misplaced a

hasNext() for ES6 Generator

How would I implement hasNext() method for a generator. I have tried many options like adding the generator as a return statement and yielding from the closure. Getting the first value printing it and then using the while etc, but none of them actually worked. I know I can use for of or while like How to loop the JavaScript

Advertisement