Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

way to add custom attributes to react component? #52

Closed
kirillsalykin opened this issue Nov 19, 2015 · 3 comments
Closed

way to add custom attributes to react component? #52

kirillsalykin opened this issue Nov 19, 2015 · 3 comments

Comments

@kirillsalykin
Copy link

Currently react has attributes whitelist, to be able to use custom attributes you can apply this workaround
(from http://stackoverflow.com/questions/31273093/how-to-add-custom-html-attributes-in-jsx):

Custom attributes are currently not supported. See this open issue for more info: facebook/react#140
As a workaround, you can do something like this in componentDidMount:
componentDidMount: function() {
this.refs.test.getDOMNode().setAttribute('custom-attribute', 'some value');
}
See https://jsfiddle.net/peterjmag/kysymow0/ for a working example. (Inspired by syranide's suggestion in this comment.)

How i can use this workaround with rum? i need to add webkitdirectory to file upload.

Thanks!

@tonsky
Copy link
Owner

tonsky commented Nov 22, 2015

(rum/defc my-comp < { :did-mount (fn [state]
                                   (let [comp (:rum/react-component state)
                                         node (js/ReactDOM.findDOMNode comp)]
                                     (.setAttribute node "custom-attribute" "value"))
                                   state) }
  [:div])

If you’re using [rum 0.5.0], replace js/ReactDOM with js/React.

For your convenience, you can abstract that into mixin:

(defn with-attributes [m]
  { :did-mount
    (fn [state]
      (let [comp (:rum/react-component state)
            node (js/ReactDOM.findDOMNode comp)]
        (doseq [[k v] m]
          (.setAttribute node (name k) v)))
      state) })

(rum/defc my-comp < (with-attributes {:custom "value", :another "value2"})
  [:div])

@kirillsalykin
Copy link
Author

wow, this is really awesome!

@kirillsalykin
Copy link
Author

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants