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

How to render html string to dom, like what we do dangerouslySetInnerHTML in react? #8

Closed
c7h12 opened this issue Feb 5, 2021 · 4 comments

Comments

@c7h12
Copy link

c7h12 commented Feb 5, 2021

I am tring nana-jsx in my new project, and meet with a predicament to render html string to dom.
Any ideas?

@yandeu
Copy link
Member

yandeu commented Feb 5, 2021

I have never used dangerouslySetInnerHTML.

Can you show me your use case?

@c7h12
Copy link
Author

c7h12 commented Feb 5, 2021

The data i got is some html strings, like let title = '<span style="color: red;">Big News</span> xxx',
and i need to render it to h1, <h1>{title}</h1>
Nano.renderSSR() works well, and when Nano.hydrate() in browser, it becomes text;

@yandeu
Copy link
Member

yandeu commented Feb 5, 2021

Try:

import { jsx } from 'nano-jsx/lib/jsx'

const App = () => {
  let title = jsx`<span style="color: red;">Big News</span> xxx`
  return <h1>{title}</h1>
}

@c7h12
Copy link
Author

c7h12 commented Feb 6, 2021

@yandeu Thanks, It helps.

import { jsx } from 'nano-jsx/lib/jsx'

const HtmlContent = ({html})=>{
  return <Fragment>{jsx([html])}</Fragment>
}


const App = ()=>{
  const html = '<span style="color: red;">Big News</span> xxx';
  return <h1><HtmlContent html={html}/></h1>;
}

@c7h12 c7h12 closed this as completed Feb 6, 2021
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