How to define constants in ReactJS

前端 未结 5 2219
无人共我
无人共我 2021-02-03 18:42

I have a function that maps text to letters:

sizeToLetterMap: function() { 
     return {
                small_square: \'s\',
                large_square: \'q\         


        
5条回答
  •  萌比男神i
    2021-02-03 19:03

    Warning: this is an experimental feature that could dramatically change or even cease to exist in future releases

    You can use ES7 statics:

    npm install babel-preset-stage-0
    

    And then add "stage-0" to .babelrc presets:

    {
        "presets": ["es2015", "react", "stage-0"]
    }
    

    Afterwards, you go

    class Component extends React.Component {
        static foo = 'bar';
        static baz = {a: 1, b: 2}
    }
    

    And then you use them like this:

    Component.foo
    

提交回复
热议问题