【问题标题】:Switch case is not working in Reactjs切换案例在 Reactjs 中不起作用
【发布时间】:2017-08-14 03:41:22
【问题描述】:

我有一个 json,我正在尝试使用 json 数据显示一个表单。我尝试使用 Switch 案例显示索引,因此将根据 html 控件类型显示索引。下面是我的代码

var React = require('react');
var ReactDOM = require('react-dom');

var DATA = {
"indexList": [{
        "Label": "Name",
        "Type": "text",
        "Regex": "",
        "Default_Val": "",
        "Values": {
            "Key": "",
            "Value": ""
        },
        "Validtion Msg": "",
        "Script": "",
        "Mandatory": "required",
        "maxLength":"16",
        "minLength":"7",
        "format":"Alphanumeric",
        "cssClassName": "form-control",
        "Placeholder": ""
    },
    {
        "Label": "Select Language",
        "Type": "dropdown",
        "Regex": "",
        "Default_Val": "English",
        "Values": [{
            "Key": "option1",
            "Value": "English"
        },{
            "Key": "option2",
            "Value": "Spanish"
        }],
        "Validtion Msg": "",
        "Script": "",
        "Mandatory": "Y",
        "maxLength":"",
        "minLength":"",
        "format":"",
        "cssClassName": "form-control",
        "Placeholder": ""
    },

    {
        "Label": "Type",
        "Field_Type": "radio",
        "Regex": "",
        "Default_Val": "",
        "Values": [{
            "Key": "option1",
            "Value": "Form1"
        }, {
            "Key": "option2",
            "Value": "Form2"
        }, {
            "Key": "option3",
            "Value": "Form3"
        },{
            "Key": "option4",
            "Value": "Form4"
        },{
            "Key": "option5",
            "Value": "Form5"
        }],
        "Validtion Msg": "",
        "Script": "",
        "Mandatory": "Y",
        "maxLength":"",
        "minLength":"",
        "format":"",
        "cssClassName": "form-control",
        "Placeholder": ""
    }
]
};

var Menu = React.createClass({

renderForm: function () {

    var data = DATA.indexList;
    console.log(data);
    return data.map(group =>{
        return <div>
                <label for={group.Label}>{group.Label}</label>
                <div>
                    switch(group.Type) {
                        case 'text':
                        return <input className={group.cssClassName} 
                                      id={group.Label} 
                                      placeholder={group.Placeholder}
                                      {group.Mandatory}/>

                        case 'dropdown':
                        return <select className={group.cssClassName}>
                                    <option value="">{group.Placeholder}</option>
                                    <option for="let values of group.Values.value">{values}</option>
                                </select>


                        case 'radio':
                        return <div className={group.Type}>
                                    <div for="let value of group.Values">
                                        <label><input
                                        name="radios"/>{value}</label>
                                    </div>
                                </div>

                        case 'chekbox'
                        return <div className={group.Type}>
                                    <div for="let value of group.Values">
                                        <label><input name="checkbox"/>{value}</label>
                                    </div>
                                </div>
                    }
                </div>
               </div>
    });
},

render: function() {
    return (    
        <div className="container">
            <br/>
            <div className="panel panel-primary">
                <div className="panel-heading">Form</div>
                    <div className="panel-body">
                        <form>
                            <div className="form-group">
                                <div className="col-xs-5">
                                    {this.renderForm()}
                                    <button type="button" className="btn btn-primary">Submit</button>
                                </div>
                            </div>
                        </form>
                    </div>      
            </div>
        </div>
)}
});

module.exports = Menu

使用上面的代码,我收到一个错误“Unexpexcted token”,错误指向“case”。任何人都可以帮助解决这个问题,我是新手,我无法解决这个问题。代码中有语法错误吗?

【问题讨论】:

    标签: forms reactjs dynamic switch-statement


    【解决方案1】:

    因为你忘了放{},所以用这个:

    <div>
     {
    
     }
    

    要在HTML 元素中使用任何javascript 代码,我们需要使用{}

    注意:我们不能直接在JSX中使用if-else/switch语句,使用ternary operator或者从JSX调用函数并在其中使用if-else/switch

    参考:http://reactjs.cn/react/tips/if-else-in-JSX.html

    检查工作示例:

    var DATA = {
    "indexList": [{
            "Label": "Name",
            "Type": "text",
            "Regex": "",
            "Default_Val": "",
            "Values": {
                "Key": "",
                "Value": ""
            },
            "Validtion Msg": "",
            "Script": "",
            "Mandatory": "Y",
            "maxLength":"16",
            "minLength":"7",
            "format":"Alphanumeric",
            "cssClassName": "form-control",
            "Placeholder": ""
        },
        {
            "Label": "Select Language",
            "Type": "dropdown",
            "Regex": "",
            "Default_Val": "English",
            "Values": [{
                "Key": "option1",
                "Value": "English"
            },{
                "Key": "option2",
                "Value": "Spanish"
            }],
            "Validtion Msg": "",
            "Script": "",
            "Mandatory": "Y",
            "maxLength":"",
            "minLength":"",
            "format":"",
            "cssClassName": "form-control",
            "Placeholder": ""
        },
    
        {
            "Label": "Type",
            "Type": "radio",
            "Regex": "",
            "Default_Val": "",
            "Values": [{
                "Key": "option1",
                "Value": "Form1"
            }, {
                "Key": "option2",
                "Value": "Form2"
            }, {
                "Key": "option3",
                "Value": "Form3"
            },{
                "Key": "option4",
                "Value": "Form4"
            },{
                "Key": "option5",
                "Value": "Form5"
            }],
            "Validtion Msg": "",
            "Script": "",
            "Mandatory": "Y",
            "maxLength":"",
            "minLength":"",
            "format":"",
            "cssClassName": "form-control",
            "Placeholder": ""
        }
    ]
    };
    
    var Menu = React.createClass({
    
    _renderElement: function(group){
       switch(group.Type){
                            case 'text':
                            return <input className={group.cssClassName} 
                                          id={group.Label} 
                                          placeholder={group.Placeholder}
                                          required={group.Mandatory == 'Y'? true: false}/>
    
                            case 'dropdown':
                            return <select className={group.cssClassName}>
                                        <option value="">{group.Placeholder}</option>
                                        {group.Values.map(el => <option key={el.Key} for="let values of group.Values.value">{el.Value}</option>)}
                                    </select>
    
    
                            case 'radio':
                            return <div className={group.Type}>
                                        <div for="let value of group.Values">
                                        {group.Values.map(el=> <label key={el.Value}><input
                                            name="radios"/>{el.Value}</label>)}
                                        </div>
                                    </div>
    
                            case 'checkbox':
                            return <div className={group.Type}>
                                        <div for="let value of group.Values">
                                            <label><input name="checkbox"/>{value}</label>
                                        </div>
                                    </div>
                        }
    },
    
    renderForm: function () {
    
        var data = DATA.indexList;
        return data.map(group =>{
            return <div>
                    <label for={group.Label}>{group.Label}</label>
                    <div>
                    {
                       this._renderElement(group)
                    }
                    </div>
                   </div>
        });
    },
    
    render: function() {
        return (    
            <div className="container">
                <br/>
                <div className="panel panel-primary">
                    <div className="panel-heading">Form</div>
                        <div className="panel-body">
                            <form>
                                <div className="form-group">
                                    <div className="col-xs-5">
                                    
                    
                                        {this.renderForm()}
                                        <button type="button" className="btn btn-primary">Submit</button>
                                    </div>
                                </div>
                            </form>
                        </div>      
                </div>
            </div>
    )}
    });
    
    ReactDOM.render(<Menu/>, document.getElementById('app'))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    
    <div id='app'/>

    【讨论】:

    • 我添加了 { },但我仍然收到相同的错误并指向“Switch”。
    • 检查更新的代码,运行上面的sn-p,我们不能直接在jsx中使用switch或if else,调用一个函数并在里面做那部分:)
    • 感谢上述工作代码。你能帮我迭代上面代码中的“值”选项吗?截至目前,它不起作用。我只想在这里显示单选按钮和下拉选项。需要选择列表值并显示相同。当前的逻辑不工作,我得到错误。你能帮忙循环这些值并显示它吗?
    • 检查更新的答案,通过使用map over values我们可以创建html元素:)
    • 您好,我已经更新了案例“文本”,我需要来自 json 的“必需”属性。截至目前,我已经像 {group.Mandatory} 一样进行了更新,但这在控制台“意外令牌”中显示了语法错误。如何解决这个问题?
    【解决方案2】:

    swithc-case 应该在大括号内

    renderForm: function() {
    
      var data = DATA.indexList;
      console.log(data);
      return data.map(group => {
        return <div >
          < label
        for = {
          group.Label
        } > {
          group.Label
        } < /label> < div >{
          switch (group.Type) {
            case 'text':
              return <input className = {
                group.cssClassName
              }
              id = {
                group.Label
              }
              placeholder = {
                group.Placeholder
              }
              />
    
            case 'dropdown':
                        return;
    
          }} < /div> < /div>
      });
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多