【问题标题】:pass an object containing html as an attribute to jsx element将包含 html 的对象作为属性传递给 jsx 元素
【发布时间】:2018-12-24 15:23:56
【问题描述】:

我有一个名为 VariantList 的主要组件,它根据我自己创建的 json 列出了一些堆叠在一起的框,单击时每个框上都有一个复制按钮,将在第一个框下方创建一个完全相同的框作为第一个。 我的问题是,当我将数据保存为 box 元素中的属性时,包含 jsx 的内部对象在创建 box 时将不可读,并抛出错误。

import React, { Component } from 'react';



class VariantList extends Component {

    constructor() {
        super();
        this.handleCopy = this.handleCopy.bind(this);
        this.state = {
            variants: [

                {
                    id: 1,
                    title: 'تنوع ۱',
                    status: {
                        number: 1,
                        title: 'تایید شده'
                    },
                    description: 'تنوع های مختص این محصول',
                    variants:  [

                        {
                            icon: 'fa-paint-brush',
                            title: 'رنگ انتخاب شده:',
                            value: <div>
                                        <span style={{ width:"12px",height:"12px",margin:"0 4px",backgroundColor:"#ff0000",borderRadius:"50%",display: "inline-block",verticalAlign: "middle" }}></span>
                                        <span>قرمز</span>
                                    </div>
                        },
                        {
                            icon: 'fa-pencil',
                            title: 'سایز:',
                            value: <span>XL</span>
                        },
                        {
                            icon: 'fa-plane',
                            title: 'گارانتی:',
                            value: <span>امرتات</span>
                        },
                        {
                            icon: 'fa-tag',
                            title: 'قیمت:',
                            value: <span>۱۲۰۰۰۰۰۰ تومان</span>
                        },
                        {
                            icon: 'fa-tint',
                            title: 'حافظه:',
                            value: <span>512 GB</span>
                        },
                        {
                            icon: 'fa-wifi',
                            title: 'تخفیف:',
                            value: <span>۲۵٪</span>
                        },
                        {
                            icon: 'fa-send',
                            title: 'هدیه:',
                            value: <span>دارد</span>
                        },
                        {
                            icon: 'fa-signal',
                            title: 'موجودی:',
                            value: <span>۱۰۰ عدد</span>
                        },
                        {
                            icon: 'fa-vcard',
                            title: 'زمان بندی:',
                            value: <span>۸ صبح پنج شنبه ۲۳ آبان</span>
                        }

                    ]
                }

            ]
        }
    }
    handleCopy(variant) {
        let variantArr = this.state.variants;
        variantArr.splice(this.state.variants.findIndex(x => x.id === variant.id),0,variant);
        this.setState({
            variants: variantArr
        });
    }

    render() {
        let variant_boxes = [];
        for (let i = 0; i < this.state.variants.length; i++) {
            variant_boxes.push(<VariantBox key={ i } data = { this.state.variants[i] } copyVariant={ this.handleCopy } index={ i+1 } />);
        }

        return (
            <div id="variantBoxContainer" className="row no-gutters">
                { variant_boxes }
            </div>
        );
    }
}

class VariantBox extends Component {

    constructor(props) {
        super(props);
        this.handleCopy = this.handleCopy.bind(this);
        this.variantRef = React.createRef();
    }

    handleCopy = () => {
        this.props.copyVariant(JSON.parse(this.variantRef.current.getAttribute('variant')));
    }
    render() {
        return(
            <div className="col-xxl-12 justify-content-center yelno-bg-white variant_box"
                variant={ JSON.stringify(this.props.data) }
                ref={this.variantRef} >
                <div className="row no-gutters">
                    <div className="col-xxl variant_badges">
                        <div className="yelno-badge variant">
                            <span className="innerBadge">{ this.props.data.title }</span>
                        </div>
                        {
                            this.props.data.status.number ?
                            "" :
                            <div className="yelno-badge inprocess">
                                <span className="innerBadge">{ this.props.data.status.title }</span>
                            </div>
                        }
                    </div>
                    <div className="col-xxl text-left action_btn_container">
                        <div className="action_btn d-inline-block">
                            <i className="fa fa-copy fa-lg" variantid={ this.props.data.id } onClick={ this.handleCopy }></i>
                        </div>
                        <div className="action_btn d-inline-block">
                            <i className="fa fa-edit fa-lg"></i>
                        </div>
                        <div className="action_btn d-inline-block">
                            <i className="fa fa-eye fa-lg"></i>
                        </div>
                        <div className="action_btn d-inline-block">
                            <i className="fa fa-trash fa-lg"></i>
                        </div>
                    </div>
                </div>
                <div className="row no-gutters variant-attributes">
                    <VariantItem data = { this.props.data.variants } />
                </div>
            </div>
        );
    }
}


class VariantItem extends Component {
    render() {
        return(
            this.props.data.map((data,index) => {
                return (
                    <div key={ index } className="col-xxl text-center">
                        <i className={ "fa " + data.icon + " fa-2x yelno-color-light-grey variant-attribute-icon" }></i>
                        <span className="variant-attribute-title d-block yelno-color-light-grey">
                            { data.title }
                        </span>
                        <div className="variant-attribute-value yelno-12BoldS">
                            {/* { data.value } */}
                        </div>
                    </div>
                );
            })
        );
    }
}

export default VariantList;

我已经评论了{/* { data.value } */},但是如果我在单击复制按钮后取消评论它,则会由于未定义的this.state.variants[i].variants[j].value 包含html 而引发错误。 非常感谢您的帮助。

【问题讨论】:

  • 看起来 variables 不是有效数组,value props 值应该是单引号
  • 400 行的代码块并没有真正邀请贡献者查看您的问题。尝试用 40 行或更少的行来解决问题...

标签: javascript html reactjs jsx


【解决方案1】:

我通过我在 cmets 中给出的想法找到了答案,所以我没有将 HTML 节点保存为 JSON 中的 JSX,而是将其保存为字符串并使用 react-html-parser 库将字符串转换为 react 组件如下:

import ReactHtmlParser from 'react-html-parser';

在 VariantList 组件中的 JSON 中编辑了这一行

value: '<div><span style="width:12px;height:12px;margin:0 4px;background-color:#ff0000;border-radius:50%;display: inline-block;vertical-align: middle; "></span><span>قرمز</span></div>'

最后在 VariantItem 组件中将 { data.value } 转换为 { ReactHtmlParser(data.value) } 并且效果很好。

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 2021-07-12
    • 2017-07-22
    • 2012-05-01
    • 2017-06-21
    • 1970-01-01
    • 2020-04-11
    相关资源
    最近更新 更多