Skip to content

Instantly share code, notes, and snippets.

@ar5had
Created June 19, 2017 12:40
Show Gist options
  • Save ar5had/e2a06e2e7127a97b994f3f1cc06f7db1 to your computer and use it in GitHub Desktop.
Save ar5had/e2a06e2e7127a97b994f3f1cc06f7db1 to your computer and use it in GitHub Desktop.

Revisions

  1. ar5had created this gist Jun 19, 2017.
    36 changes: 36 additions & 0 deletions ReactComponentWithCustomAttributes.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    // Suppose we want a div to have some custom attributes, normally react wouldn't allow custom attributes
    // so this is a hack to att as much custom attributes to your component as you want

    class Div extends React.Component {

    componentDidMount() {
    this.addAttributes();
    }

    componentWillReceiveProps(nextProps) {
    this.addAttributes();
    }

    addAttributes() {
    this.props.attributes.forEach(attr => (this.button.setAttribute(attr.name, attr.value)));
    }

    render() {
    const {className, id} = this.props;
    return (
    <div className={className} id={id}>
    {this.props.content}
    </div>
    );
    }
    }


    // Now use Div component

    const attributesObject = {
    customAttr1: "attr1",
    customAttr2: "attr2"
    };

    <Div attributes={attributesObject} className="mydiv" id="someid" content="This is div body content"/>