【问题标题】:React function return containing dynamic html is not rendering包含动态 html 的 React 函数返回未呈现
【发布时间】:2017-01-25 23:29:07
【问题描述】:

所以我有一个函数可以过滤传递的参数是选择、文本还是日期字段,并动态添加到渲染 jsx。

当我触发返回时,它不会呈现 html/jsx 返回。我在 console.logs 而不是 html 中进行了测试,它成功了,这告诉我 switch 语句的结构是正确的,并且我传递了正确的类型,只是 html 返回不想呈现。没有警告或错误。当我控制台记录 checkType 函数时,我得到了

没有警告或错误。

这是this.getFields()传过来的数据图片来验证

// wrapped in a react class

checkType(type, options, placeholder, name, handleUpdatedValue, defvalue, index) {

        let select = <select onChange={handleUpdatedValue.bind(this)} >{options.map((option, i) => <option value={option} key={i}>{option}</option>)}</select>;
        let text = <input onChange={handleUpdatedValue.bind(this)} name={name} placeholder={placeholder} type="text" />
        let date = <input onChange={handleUpdatedValue.bind(this)} name={name} placeholder={placeholder} type="date" />

        switch(type) {
            case 'select':
                return select
                break;
            case 'text':
                return text
                break;
            case 'date':
                return date
                break;
            default: 
                console.log('Error: Invalid Type');
        }
    }

handleSubmit() {

}

render() {

    let values = this.state.fieldValues;
    const checkType = this.checkType.bind(this);

    return(
        <div className="formwrapper thinshadow">    
            <h3>{this.props.header}</h3>
            {this.getFields().map((field, i) => {
                <div key={i} className={field.classes}>
                    {checkType(field.type, field.options, field.placeholder, field.name, this.handleUpdatedValue.bind(this), field.defvalue, field.index)}
                    <div className="minilabel"></div>
                </div>
            })}

            <button className="btn btn-primary" 
                    onClick={() => this.props.onClick(values)} >
                    Save
            </button>
        </div>  
    );
}

【问题讨论】:

  • 您的检查员显示什么? DOM 树中是否有以下元素 &lt;div key={i} className={field.classes}&gt;
  • @Gasim 它没有,太奇怪了。只有

    标签和按钮

  • 您的问题似乎不是checkType,而是您的getFields 方法返回一个空数组。所以,我会研究getFields方法的定义。
  • @Gasim 我刚刚做了一个console.log,它传递了一个包含所有预期对象的数组:(我将发布传递数据的图片。
  • 我找到了你的问题

标签: javascript reactjs jsx


【解决方案1】:
{this.getFields().map((field, i) => {
         <div key={i} className={field.classes}>
                 {checkType(field.type, field.options, field.placeholder, field.name, this.handleUpdatedValue.bind(this), field.defvalue, field.index)}
                 <div className="minilabel"></div>
         </div>
 })}

您的代码不会返回任何内容,因为您在函数语法中使用了花括号。要么做

{this.getFields().map((field, i) =>
         <div key={i} className={field.classes}>
                 {checkType(field.type, field.options, field.placeholder, field.name, this.handleUpdatedValue.bind(this), field.defvalue, field.index)}
                 <div className="minilabel"></div>
         </div>
 )}

{this.getFields().map((field, i) => {
        return (
            <div key={i} className={field.classes}>
                    {checkType(field.type, field.options, field.placeholder, field.name, this.handleUpdatedValue.bind(this), field.defvalue, field.index)}
                    <div className="minilabel"></div>
            </div>
        );
 })}

对于干净的代码,我会将 map 函数保留在 JSX 标签之外:

render() {
    let values = this.state.fieldValues;
    const checkType = this.checkType.bind(this);

        const fields = this.getFields().map((field, i) =>
      <div key={i} className={field.classes}>
          {checkType(field.type, field.options, field.placeholder, field.name, this.handleUpdatedValue.bind(this), field.defvalue, field.index)}
          <div className="minilabel"></div>
      </div>
        );

    return(
        <div className="formwrapper thinshadow">    
            <h3>{this.props.header}</h3>
                        {fields}

            <button className="btn btn-primary" 
                    onClick={() => this.props.onClick(values)} >
                    Save
            </button>
        </div>  
    );
}

【讨论】:

  • 哦,天哪……我很敬畏……谢谢@Gasim 也谢谢你的建议。将实施后一种方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多