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

Could dangerouslySetInnerHTML.__html be null or undefined? #3460

Closed
victor-homyakov opened this issue Mar 19, 2015 · 6 comments · Fixed by #3727
Closed

Could dangerouslySetInnerHTML.__html be null or undefined? #3460

victor-homyakov opened this issue Mar 19, 2015 · 6 comments · Fixed by #3727

Comments

@victor-homyakov
Copy link
Contributor

In following JSX fragment I should guarantee that this.props.description will never be null or undefined:

<div dangerouslySetInnerHTML={{__html: this.props.description}}></div>

Otherwise https://github.com/facebook/react/blob/master/src/browser/ui/ReactDOMComponent.js#L74 throws an error although construction is correct:

`props.dangerouslySetInnerHTML` must be in the form `{__html: ...}`

Should invariant instead of props.dangerouslySetInnerHTML.__html != null check for '__html' in props.dangerouslySetInnerHTML?

@sophiebits
Copy link
Collaborator

Someone else mentioned this being surprising to me too.

@chollier
Copy link

I've been experiencing the same issues recently

@syranide
Copy link
Contributor

As far as I understand it, the idea is that {__html: ...} is basically the equivalent of a HTML-type. You're not meant to use it as <div dangerouslySetInnerHTML={{__html: this.props.description}} />, but as <div dangerouslySetInnerHTML={this.props.description} /> so that you cannot accidentally pass unsafe data. So description should be null or {__html: ...}, in this model it does not make sense to have {__html: null}.

@sophiebits
Copy link
Collaborator

@syranide That doesn't mean that people won't write it as dangerouslySetInnerHTML={{__html: this.props.description}} – doing so is even pretty reasonable if you're careful about naming your vars/props and do dangerouslySetInnerHTML={{__html: this.props.descriptionHTML}}.

@glenjamin
Copy link
Contributor

The simplest workaround is to coerce into an empty string with dangerouslySetInnerHTML={{__html: (this.props.description || '')}}.

+1 for having React do this internally.

@nicolashery
Copy link

Got surprised by this today, upgrading from react 0.12.2 to react 0.13.2.

Was it a "bug" in 0.12 (i.e. it shouldn't have accepted {__html: null}), or is it a bug in 0.13?

Anyway, will use {__html: (this.props.description || '')} as suggested by @glenjamin, just wanted to point out that it seemed to "work" in 0.12.

sophiebits added a commit to sophiebits/react that referenced this issue Apr 22, 2015
framp pushed a commit to framp/react that referenced this issue May 26, 2015
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

Successfully merging a pull request may close this issue.

6 participants