3

This is regarding non-standard attributes. https://facebook.github.io/react/docs/tags-and-attributes.html

In react I have done this:

 React.createElement('div', {image:'blah', etc:'blah'});

I need image and etc to be set on the element with setAttribute, and I need react to use its smarts to maintain it as it changes.

A solution here https://stackoverflow.com/a/21654914/1828637 says to add it on componentDidMount but this is not a solution. The attribute will not be maintained as it changes by the react framework.

Is there anyway to tell react to set the attribute on my custom tags?

3 Answers 3

4

In react 16 custom attributes are now possible

// Your code:
<div mycustomattribute="something" />

// React 15 output:
<div /> 

// React 16 output:
<div mycustomattribute="something" />

react 16 custom attributes

Sign up to request clarification or add additional context in comments.

Comments

2

This solution is to build on the linked answer by using the React lifecycle method componentWillReceiveProps to update the DOM element attributes with every change to props. For more information about all the lifecycle methods, see http://facebook.github.io/react/docs/component-specs.html.

(Since componentWillReceiveProps can be called more often than when the props actually change, you may wish to compare the props before actually setting them on the node.)

I've provide fiddle you can play with: https://jsfiddle.net/p4h267bo/ and the relevant part of the code is excerpted here:

var Hello = React.createClass({
  componentDidMount() {
    this.mirrorProps(this.props);
  },
  componentWillReceiveProps(nextProps) {
    this.mirrorProps(nextProps);
  },
  mirrorProps(props) {
    var node = ReactDOM.findDOMNode(this);
    node.setAttribute('name', props.name);
  },
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

8 Comments

Wow thank you so much for such a quick answer and it was superb! It worked fantastic I updated it to shouldMirrorProps in this fork of your fiddle - jsfiddle.net/Noitidart/wwLcbvfk - your fiddle helped me out immmmensely! This is a great answer I wish I could up vote you more so I fond your other answers and comments up voted them.
Will this work with componentWillUpdate as well? Or is componentWillUpdate only called if it is calculated that the DOM will change? Because some stuff I have in this.state that I want to mirror as attributes.
Now that I think about it, it might be better to do it in componentDidUpdate, because at that point you will know that any changes to the DOM have been flushed (in case you have some conditionals that would result in the DOM node going away — the current implementation only works because the node is stable). Here's another fiddle!
I forgot to mention, if you are doing any kind of shouldComponentUpdate checks, you may want to include both componentWillReceiveProps and componentDidUpdate to be sure. componentWillReceiveProps still has access to this.state, and state updates will trigger componentDidUpdate.
Unless you use shouldComponentUpdate to short-circuit the update after setState, React will still call componentDidUpdate.
|
2

Another alternative is to change the name of the attribute to something that react supports (such as the data-* attributes) :

render() {
    return (
      <div data-image='blah' data-etc='blah' />
    );
}

link to other supported attributes: https://facebook.github.io/react/docs/dom-elements.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.