3

I am trying to return an element with an inline switch. But I am getting only a empty <span> </span>. What am I doing wrong?

 getRowTdForHeader: (header: string, entry: response) => {
            return (<span>
                {() => {
                    switch (header) {
                        case "Name":  return entry.name;
                        case "Type": return entry.type;
                        default: return entry.name;
                    }
                } }
            </span>);
        }

Result is a empty span and if I put a breakpoint at the return, it never goes into the switch statement at all. I'd like to keep it as a in-liner if possible. The switch works fine if I don't inline it btw.

5
  • First, I don't see purpose for ()=> Second, even switch is not required. You can do entry[(header || 'name')] Commented Jan 24, 2017 at 10:04
  • @Rajesh How does this work entry[(header || 'name')]? Commented Jan 24, 2017 at 10:11
  • 1
    entry is an object and since you are returning property that is same word but different case, header.toLowercase() will give you property name. So if header is Name, entry[header.toLowerCase()] will return entry['name']. || 'name' is the default value. If header is not defined, return name. Also my apologies, I forgot .toLowerCase() Commented Jan 24, 2017 at 10:15
  • @Rajesh Alright thanks. But the header names do not necessarily reflect the property name. Commented Jan 24, 2017 at 10:17
  • But will case always have return value only? If yes, I'd suggest to create a map and then just return entry[map[header]] Commented Jan 24, 2017 at 10:19

3 Answers 3

7

You have to wrap the arrow function in parentheses and execute it immediately with a following pair of parentheses.

{(() => {
    switch (header) {
                        case "Name":  return entry.name;
                        case "Type": return entry.type;
                        default: return entry.name;
                    }
})()}
Sign up to request clarification or add additional context in comments.

2 Comments

This syntax worked. Could you explain it a bit more?
1

you defined a function but not call it

() => {
    switch (header) {
        case "Name":  return entry.name;
        case "Type": return entry.type;
        default: return entry.name;
    }
}

2 Comments

How do I call it?
as @Vladu answered
1

I suggest that you put the result into a variable and then use it:

getRowTdForHeader: (header: string, entry: response) => {
    let str: string;
    switch (header) {
        case "Name":  str = entry.name;
        case "Type": str = entry.type;
        default: str = entry.name;
    }

    return <span> { str } </span>;
}

6 Comments

I guess I could do it this way, but I was curious about the inline functions.
It feels that using a function in this case is redundant. You have nothing to gain from it.
My main purpose was to use the switch inside the <span> tags. No idea how else I would go about this without a function call?
Why does the switch need to be inside the <span>? The conversion between the header to a string doesn't even have to happen in this method, you can have a static/global function which does that for you.
Does not need to be there, but for learning purposes on how I would go about inline functions.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.