最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - using switch statement for react rendering - Stack Overflow

programmeradmin6浏览0评论

I'm currently doing this to conditionally render certain Components:

render() {
    return (
        <React.Fragment>
            <div id="call_notes_app" className="row">
                <NavTree onNavChange={this.changeActiveGroup} />
                {this.state.shownGroup == 1 && <DiscoveryGroup/>}
                {this.state.shownGroup == 2 && <FinancialGroup/>}
                {this.state.shownGroup == 3 && <SalesStuffGroup/>}
            </div>
        </React.Fragment>
    );
}

When I try to use a switch statement, it doesn't work (I get ERROR ABORT in the console):

render() {
    return (
        <React.Fragment>
            <div id="call_notes_app" className="row">
                <NavTree onNavChange={this.changeActiveGroup} />

                {
                    switch(this.state.shownGroup) {
                        case 1:
                            <DiscoveryGroup/>
                            break;
                        case 2:
                            <FinancialGroup />
                            break;
                        default:
                            <SalesStuffGroup />
                    }
                }
            </div>
        </React.Fragment>
    );
}

Is there a way to do this using switch?

I'm currently doing this to conditionally render certain Components:

render() {
    return (
        <React.Fragment>
            <div id="call_notes_app" className="row">
                <NavTree onNavChange={this.changeActiveGroup} />
                {this.state.shownGroup == 1 && <DiscoveryGroup/>}
                {this.state.shownGroup == 2 && <FinancialGroup/>}
                {this.state.shownGroup == 3 && <SalesStuffGroup/>}
            </div>
        </React.Fragment>
    );
}

When I try to use a switch statement, it doesn't work (I get ERROR ABORT in the console):

render() {
    return (
        <React.Fragment>
            <div id="call_notes_app" className="row">
                <NavTree onNavChange={this.changeActiveGroup} />

                {
                    switch(this.state.shownGroup) {
                        case 1:
                            <DiscoveryGroup/>
                            break;
                        case 2:
                            <FinancialGroup />
                            break;
                        default:
                            <SalesStuffGroup />
                    }
                }
            </div>
        </React.Fragment>
    );
}

Is there a way to do this using switch?

Share Improve this question asked Apr 11, 2018 at 20:42 JacobIRRJacobIRR 8,9468 gold badges44 silver badges72 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 15

{} is for a single statement. A cleaner way would be switching before the return statement:

render() {
    let ponent = null;
    switch(this.state.shownGroup) {
      case 1:
        ponent = <DiscoveryGroup />;
        break;
      case 2:
        ponent = <FinancialGroup />;
        break;
      default:
        ponent = <SalesStuffGroup />;
    }

    return (
        <React.Fragment>
            <div id="call_notes_app" className="row">
                <NavTree onNavChange={this.changeActiveGroup} />
                {ponent}
            </div>
        </React.Fragment>
    );
}

If you eventually have many dynamic ponents, splitting things out to seperate methods cleans things up even more:

renderGroup(group) {
    switch (group) {
      case 1:
        return <DiscoveryGroup />;
      case 2:
        return <FinancialGroup />;
      default:
        return <SalesStuffGroup />;
    }
}

render() {
    return (
        <React.Fragment>
            <div id="call_notes_app" className="row">
                <NavTree onNavChange={this.changeActiveGroup} />
                {this.renderGroup(this.state.shownGroup)}
            </div>
        </React.Fragment>
    );
}

React requires expressions in the {} block, therefore you need to wrap the switch statement into an IIFE:

 {(() => {
   switch(this.state.shownGroup) {
     case 1:
         return <DiscoveryGroup/>
         break;
     case 2:
         return <FinancialGroup />
         break;
     default:
         return <SalesStuffGroup />
   }
 })()}

But i personally would actually prefer an Immeadiately Accessed Array Literal (IAAL):

 {[,<DiscoveryGroup/>, <FinancialGroup />][this.state.showGroup] || <SalesStuffGroup />}

The switch is nice, but in these cases it might be more elegant to use an object (or array with find):

const LayoutComponents {
    'simple': SimpleLayout,
    'wild': WildLayout,
    'freudian': FreudianLayout,
}


const Layout = LayoutComponents[props.layout];

if (!Layout) {
   console.error('Drama Alert');
   // throw error or return
}

// render. 
return <Layout {...props}>{props.children}</Layout>
发布评论

评论列表(0)

  1. 暂无评论