Skip to content

Symbols for constants #779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brandondewitt opened this issue Sep 22, 2015 · 8 comments
Closed

Symbols for constants #779

brandondewitt opened this issue Sep 22, 2015 · 8 comments
Labels

Comments

@brandondewitt
Copy link

I like the idea of using Symbols for action types...opinions?

export const ADD_TODO = Symbol();
export const DELETE_TODO = Symbol();
export const EDIT_TODO = Symbol();
export const COMPLETE_TODO = Symbol();
export const COMPLETE_ALL = Symbol();
export const CLEAR_COMPLETED = Symbol();
@simplesmiler
Copy link

Action types should be serializable, and symbols are not.

@gaearon
Copy link
Contributor

gaearon commented Sep 23, 2015

Yeah. You can do this technically but we don't encourage this because action types will disappear in the logs or if you persist them like Redux DevTools does.

@wmertens
Copy link
Contributor

I've been thinking about using a little helper function that takes an array
of strings and outputs a constants object of String->String.
The twist is that for production builds, it would use the array indices as
values, String->Int.

I wonder if it will make any difference in performance though. Probably not
worth bothering.

On Wed, Sep 23, 2015, 02:34 Dan Abramov [email protected] wrote:

Closed #779 #779.


Reply to this email directly or view it on GitHub
#779 (comment).

Wout.
(typed on mobile, excuse terseness)

@brandondewitt
Copy link
Author

@gaearon Thanks, I guess I would have noticed that if I were using the DevTools. Next on my list.

@jameswomack
Copy link

@wmertens Did you mean something like this: https://www.npmjs.com/package/keykey ?

@jjwill
Copy link

jjwill commented Jun 1, 2017

@brandondewitt I was using Symbols to reduce the user-error of having action types with the same name. When it happens it can be hard to track (most times it's obvious).

This may work if you need to keep your action types uniq:

export const getUniqueType = (name) => {
  class UniqueType {
    static fromJSON = (name) => new UniqueType(name)

    constructor(name) {
      this.name = name;
    }

    toString() {
      return this.name;
    }

    toJSON() {
      return this.name;
    }    
  }
  return new UniqueType(name);
};

It there a better way to handle this?

@markerikson
Copy link
Contributor

markerikson commented Jun 1, 2017

@jjwill : That's probably going to be an issue for the same reason as symbols. See the Redux FAQ on action type constants for more info.

There's a wide variety of utilities out there for generating action constants - check out the Actions and Action/Reducer Generators sections of my Redux addons catalog for links to libs that are out there.

@jjwill
Copy link

jjwill commented Jun 1, 2017

Thanks @markerikson I'll check those links out! The function above works for the debugging tools. We were using Symbols but after installing the debugging tools had to implement that for readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants