43

In my ReactJS-based application I do:

var _ = React.DOM;
_.span(null, 'some text', _.select(null, ...));

The problem is: 'some text' is wrapped in additional span element in the DOM. Is there any way to avoid this behavior and just output raw text?

To be clear: I want to output

<span>some text<select>...</select></span>

not

<span><span>some text</span><select>...</select></span>
7
  • 1
    Looking at the React Docs. the method "span" is intended to create a <span>. Maybe try using the DOM.text() method? Commented Jun 28, 2014 at 17:57
  • No, text outputs <text>. To be clear: I want to output <span>some text<select>...</select></span> not <span><span>some text</span><select>...</select></span>. Commented Jun 28, 2014 at 17:59
  • 3
    It's not possible. React needs to be able to give a unique ID to every piece of content, and text that is a sibling of another element is not addressable because Text nodes have no attributes. Commented Jun 28, 2014 at 18:28
  • What are you trying to do that the <span> prevents? Commented Jun 28, 2014 at 18:30
  • @ssorallen post that as an answer :-) Commented Jun 28, 2014 at 18:30

4 Answers 4

40

Update: This is now "fixed" in React v15 (2016-04-06) – now comment nodes are added around each piece of text, but it is no longer wrapped in a <span> tag.

We received some amazing contributions from the community in this release, and we would like to highlight this pull request by Michael Wiencek in particular. Thanks to Michael’s work, React 15 no longer emits extra <span> nodes around the text, making the DOM output much cleaner. This was a longstanding annoyance for React users so it’s exciting to accept this as an outside contribution.

Full release notes.


This is currently a technical limitation of React; it wraps any floating text nodes in a span so that it can assign it an ID and refer back to it later. In a future version of React hopefully we can remove this restriction.

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

3 Comments

See also #1236 which is a related issue.
Where there's a will there's a way. Couldn't insert code here in the comment field, so added one below.
@Kokodoko Then add a span around them explicitly.
2

You can hard code the html as a last resort.

<option value={value} dangerouslySetInnerHTML={{__html: value}}></option>

1 Comment

<option value={value}>{value}</option> already doesn't add a span.
1

Well.. If you're hell bent on doing this, and accept the limitation that you cannot access props or state, you could do this:

var Component = React.createClass({
    displayName:"Statics",
    statics: {
         customRender: function(foo) {
              return React.renderToStaticMarkup(<div 
              dangerouslySetInnerHTML={{__html: foo.bar }}/>);
         }
    },
    render: function () {
        return <div dangerouslySetInnerHTML={{__html: 
               <Component.customRender bar="<h1>This is rendered
               with renderToStaticMarkup</h1>" />}} />
    }
});

renderToStaticMarkup will not insert any spans or react-dataid, and is meant for static server rendering. It's probably not a great idea to do this, but there you go.

renderToStaticMarkup Similar to renderToString, except this doesn't create extra DOM attributes such as data-react-id, that React uses internally. This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save lots of bytes.

Check the result at: http://learnreact.robbestad.com/#/static

1 Comment

If you just render <h1>This is rendered without renderToStaticMarkup</h1>, that doesn't make any spans either.
0

I Changed the version of react and react-dom and worked perfect

"react": "^15.0.1",
"react-dom": "^15.0.1"

1 Comment

Yep, React >= 15 doesn't create those spans anymore.

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.