Skip to content

Commit

Permalink
Merge pull request #1 from christo-pr/bug/rerender-after-html-prop-ch…
Browse files Browse the repository at this point in the history
…ange

handle re-render on prop change, and avoid duplicate content
  • Loading branch information
christo-pr authored Feb 17, 2020
2 parents e064922 + 04d7e89 commit d75bcf5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
15 changes: 11 additions & 4 deletions example/src/App.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import React from 'react'
import React, { useState } from 'react'
import InnerHTML from 'dangerously-set-html-content'

export default function App() {

const html = `
const [content, setContent] = useState(`
<div>This wil be rendered</div>
<script>
alert('testing')
</script>
`
)
const updatedHtml = `
<div>Look at the console now!</div>
<script>
console.log('hacked!')
</script>
`

return (
<div>
<InnerHTML html={html} />
<InnerHTML html={content} />
<button onClick={() => setContent(updatedHtml)}>Hit here to see magic!</button>
</div>
);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dangerously-set-html-content",
"version": "1.0.2",
"version": "1.0.3",
"description": "Render raw html at your own risk!",
"author": "christo-pr",
"license": "MIT",
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ function DangerouslySetHtmlContent(props) {

useEffect(() => {
const slotHtml = document.createRange().createContextualFragment(html) // Create a 'tiny' document and parse the html string
divRef.current.appendChild(slotHtml) // Append it so it can be executed
}, [])
divRef.current.innerHTML = '' // Clear the container
divRef.current.appendChild(slotHtml) // Append the new content
}, [html])


return (
Expand Down

0 comments on commit d75bcf5

Please sign in to comment.