String interpolation on variable

With something as simple as ${str} you can use a simple string replacement: var template = (tpl, args) => tpl.replace(/\${(\w+)}/g, (_, v) => args[v]); var tpl=”Hello ${str} and ${other}”; console.log(template(tpl, {str: ‘foo’, other: ‘bar’})); In a general case, no, not possible without eval (short of writing your own js interpreter), because ${…} can contain arbitrary … Read more

How to nest template strings in ES6?

Yes, it’s possible, but you had for some reason put the )}) part (that closes the require call, the interpolated value, and the CSS url) in the wrong place: { background: `url(${require(`../../assets/${edge.node.name.toLowerCase()}.png`)}) center no-repeat` // ^^^ } That said, it’s probably a bad idea as it doesn’t exactly make the code readable. Better use a … Read more

Webpack doesn’t resolve properly my alias

Resolving the alias to the absolute path should do the trick: resolve: { alias: { myApp: path.resolve(__dirname, ‘src’), }, extensions: [”, ‘.js’, ‘.jsx’] } Check this webpack resolve alias gist with a simple example. Another solution to limit the number of relative paths is to add your ./src folder as root instead of aliasing it: … Read more

Why do I have to .bind(this) for methods defined in React component class, but not in regular ES6 class

The value of this primarily depends on how the function is called. Given d.speak();, this will refer to d because the function is called as an “object method”. But in <div>{this.renderElements}</div> you are not calling the function. You are passing the function to React which will call it somehow. When it is called, React doesn’t … Read more

Uncaught TypeError: Cannot destructure property `name` of ‘undefined’ or ‘null’

See the docs: Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed. In other words, the default parameter will not be assigned if null gets passed: function fn(arg = ‘foo’) { console.log(arg); } fn(null); Destructure in the first line of the function instead: function test … Read more

Object.keys using numbers in typescript

All keys are strings in JavaScript – just use map: const sizes: number[] = Object.keys(foo).map(Number); This’ll only work for numeric strings – if it’s a European string involving decimals, for instance, it’ll be NaN, and that never ends well. console.log(Number(“10,7”)); Or change either of your types: const sizes: string[] = Object.keys(foo); Or: type Foo = … Read more

Can you, or should you use localStorage in Redux’s initial state?

Redux createStore 2nd param is intended for store initialization: createStore(reducer, [initialState], [enhancer]) So you can do something like this: const initialState = { id: localStorage.getItem(‘id’), name: localStorage.getItem(‘name’), loggedInAt: null }; const store = createStore(mainReducer, initialState); Since reducers should be pure functions (i.e. no side effects) and localStorage.setItem is a side effect, you should avoid saving … Read more

Syntax: const {} = variableName, can anyone explain or point me into the right direction [duplicate]

First of all, this has nothing to do with React. It’s part of ECMAScript 6 (or JavaScript 2015, if you prefer). What you see here is called Destructuring assignment: const {girls, guys, women, men} = state; // Is the same as const girls = state.girls; const guys = state.guys; const women = state.women; const men … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)