arrow-functions

ES6 functions, arrow functions and 'this' in an ES6 class [duplicate]

本小妞迷上赌 提交于 2019-11-27 22:35:36
This question already has an answer here: Should I write methods as arrow functions in Angular's class 3 answers Arrow vs classic method in ES6 class 1 answer class App extends Component { constructor(props) { ... } onChange = (e) => this.setState({term: e.target.value}) onSubmit(e){ e.preventDefault(); const api_key = "C1hha1quJAQZf2JUlK"; const url = `http://api.giphy.com/v1/gifs/search?q=${this.state.term}&api_key=${api_key}`; } render() { return ( <div> <form onSubmit={this.onSubmit}> <input value={this.state.term} onChange={this.onChange}/> <button>Search!</button> </form> </div> ); } }

Is it possible to export Arrow functions in ES6/7?

本秂侑毒 提交于 2019-11-27 20:20:59
The export statement below gives a syntax error export default const hello = () => console.log("say hello") why ? I'm only able to export named functions export function hello() { console.log("hello") } What is the reason? Is it possible to export Arrow functions in ES6/7? Yes. export doesn't care about the value you want to export. The export statement below gives a syntax error ... why? You cannot have a default export and give it a name ("default" is already the name of the export). Either do export default () => console.log("say hello"); or const hello = () => console.log("say hello");

Performance penalty of creating handlers on every render with react-hooks

喜你入骨 提交于 2019-11-27 16:01:34
I'm currently very amazed about the use cases of the new react hooks API and what you can possibly do with it. A question that came up while experimenting was how expensive it is to always create a new handler function just to throw it away when using useCallback . Considering this example: const MyCounter = ({initial}) => { const [count, setCount] = useState(initial); const increase = useCallback(() => setCount(count => count + 1), [setCount]); const decrease = useCallback(() => setCount(count => count > 0 ? count - 1 : 0), [setCount]); return ( <div className="counter"> <p>The count is

How to use arrow function with || operator

a 夏天 提交于 2019-11-27 15:36:42
Using Babel, I can see that callback = () => {}; compiles to callback = function callback() {}; which is what I expect. However I get an error when I try to use it with || callback = callback || () => {} Which I'd expect to be equivalent to callback = callback || function(){}; Why is this an error? Also, is there a more correct ES6 version of this familiar syntax? It fails because that is just not valid syntax. Use the following to make it work: callback = callback || (() => {}) If you don't wrap it that way, it would be interpreted as if you typed the following. But that is invalid syntax.

Arrow vs classic method in ES6 class

元气小坏坏 提交于 2019-11-27 15:15:35
Is there any reason to write classic syntax of ES6 methods? class MyClass { myMethod() { this.myVariable++; } } When I use myMethod() as callback on some event, I must write something like this (in JSX): // Anonymous function. onClick={() => { this.myMethod(); }} // Or bind this. onClick={this.myMethod.bind(this)} But if I declare method as arrow function: class MyClass { myMethod = () => { this.myVariable++; } } than I can write just (in JSX): onClick={this.myMethod} The feature you are using is not part of ES6. It's the class fields proposal . It allows you to initialize instance properties

Why is `throw` invalid in an ES6 arrow function?

风流意气都作罢 提交于 2019-11-27 14:57:30
I'm just looking for a reason as to why this is invalid: () => throw 42; I know I can get around it via: () => {throw 42}; If you don't use a block ( {} ) as body of an arrow function , the body must be an expression : ArrowFunction: ArrowParameters[no LineTerminator here] => ConciseBody ConciseBody: [lookahead ≠ { ] AssignmentExpression { FunctionBody } But throw is a statement , not an expression. In theory () => throw x; is equivalent to () => { return throw x; } which would not be valid either. You can't return throw this is effectively what you're trying to do: function(){ return throw 42

Javascript ES6, why I can not use `new` with arrow function? [duplicate]

≡放荡痞女 提交于 2019-11-27 14:25:01
问题 This question already has answers here : When should I use Arrow functions in ECMAScript 6? (9 answers) Closed 3 years ago . As far as I know, arrow function is similar to normal function. There is no problem when I use like this: let X = () => {}; let Y = function() {}; X(); Y(); However, error occurred when I use them with new let X = () => {}; let Y = function() {}; x = new X(); y = new Y(); Uncaught TypeError: X is not a constructor Would you please explain me why? Many thanks. 回答1: You

When building classes in coffeescript, is there ever a reason to not use the fat arrow for the instance methods?

柔情痞子 提交于 2019-11-27 13:43:01
When building classes in coffeescript, is there ever a reason to not use the fat arrow for instance methods? Edit: Ok then! Great reply! :) To sum up, the problems are: - Takes more memory - Inability to patch - Begs question, why is it used for this method? Convention: - Be explicit when binding functions. - Declare fat arrowed methods in the constructor. - Use as much as you want, just not in class declarations. Yes, there are reasons to not use the fat arrows always. In fact i'd argue in favour of never using fat-arrowed methods :) Thin-arrow and fat-arrow methods are conceptually different

Immediate function using JavaScript ES6 arrow functions

隐身守侯 提交于 2019-11-27 12:20:45
Does anyone know how to write an immediate function using ES6 arrow syntax? Here's the ES3/5 way of doing it: (function () { //... }()); I've tried the following but get an unexpected token error on the last line. (() => { //... }()); You can test this here: http://www.es6fiddle.net/hsb8bgu4/ From the Arrow functions examples , (() => "foobar")() // returns "foobar" So, the function invocation operator should be outside. (() => { //... })(); Sample: http://www.es6fiddle.net/hsb8s1sj/ Here is my demo codes! Always remember that function_name + () === function_caller /* ES5 */ // normal function

Why does the logical or operator (||) with an empty arrow function (()=>{}) cause a SyntaxError?

徘徊边缘 提交于 2019-11-27 09:45:34
In my code, I have something that boils down to this: var x = y || ()=>{}; (In case you are wondering, I am later calling x() and y may be defined as a function or it might not, so I don't want a TypeError to be thrown if it is not.) For some reason, this causes a SyntaxError: Unexpected token ) Why? I found out that var x = y || (()=>{}); works just fine, but y || ()=>{} dosen't work. Is this specced, or a bug in V8 or Chrome? (I tested this only in the latest release of Chrome stable.) This is normal. Unlike a function expression, which is a PrimaryExpression like other literals and