How to define constants in ReactJS

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

I have a function that maps text to letters:

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


        
5条回答
  •  灰色年华
    2021-02-03 18:54

    If you want to keep the constants in the React component, use statics property, like the example below. Otherwise, use the answer given by @Jim

    var MyComponent = React.createClass({
        statics: {
            sizeToLetterMap: {
                small_square: 's',
                large_square: 'q',
                thumbnail: 't',
                small_240: 'm',
                small_320: 'n',
                medium_640: 'z',
                medium_800: 'c',
                large_1024: 'b',
                large_1600: 'h',
                large_2048: 'k',
                original: 'o'
            },
            someOtherStatic: 100
        },
    
        photoUrl: function (image, size_text) {
            var size = MyComponent.sizeToLetterMap[size_text];
        }
    

提交回复
热议问题