question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alternative syntax using `this`?

See original GitHub issue

An alternate, more “method”-y API would be to use this as the state variable, which would mean you could provide a simple record instead of a function of state:

const initialState = { count: 0 };

const methods = {
  reset() {
    return initialState;
  },
  increment() {
    this.count++;
  },
  decrement() {
    this.count--;
  },
};

Another advantage is that VSCode gives this special emphasis and color, so it’s easy to tell at a glance where state access and “mutation” is happening.

Downsides of this approach:

  • Can’t use arrow functions to define methods
  • Can’t decide to use a shorter name for the state variable, e.g. s
  • Can’t compute variables from state that will be in scope for all methods, e.g.:
    const methods = state => {
      const { x, y, z } = state;
      return {
        // ... method definitions
      }
    };
    

I’m curious if others have any opinions about this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
TedDriggscommented, Mar 27, 2019

I like it as-is (without this), partially because we use a lint that disallows this outside class bodies, and partially because this is a piece of JS magic that always seems suspicious.

1reaction
pelotomcommented, Mar 27, 2019

Yeah, this would always be bound, because we wouldn’t be using classes, just generating a bunch of functions which come pre-bound to their context.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Alternative syntax for control structures - Manual - PHP
PHP offers an alternative syntax for some of its control structures; namely, if , while , for , foreach , and switch ....
Read more >
php - When, where & why should I use the "alternative syntax ...
I typically only use it when mixing HTML and PHP, so just for the sake of argument, here is an example to compare...
Read more >
Alternative Syntax - Ruby for Beginners
Strings. First, Ruby has an alternative syntax for defining strings that goes like so: %[any-character]The actual string[the same ...
Read more >
Alternate PHP Syntax for View Files : CodeIgniter User Guide
Alternate PHP Syntax for View Files. If you do not utilize CodeIgniter's template engine, you'll be using pure PHP in your View files....
Read more >
Alternative syntax for control structures - PHP - Webdev
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found