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

Don't use HTML strings with React #12

Open
oliverjam opened this issue Jun 9, 2022 · 1 comment
Open

Don't use HTML strings with React #12

oliverjam opened this issue Jun 9, 2022 · 1 comment

Comments

@oliverjam
Copy link

if (category === "all" && releaseYear >= min && releaseYear <= max) {
list += /*html*/ `
<li>

return <ul>{parse(list)}</ul>;

Using strings of HTML like this is not a good idea in React. For a start (as you noticed) it forces you to bring in some other library that can understand HTML. Also React already supports rendering DOM elements natively, so this isn't necessary.

Instead of building up a string of HTML you can build up an array of React Elements using JSX:

let list = [];
for (...) {
  list.push(<li>...</li>)
}
return <ul>{list}</ul>

React can handle an array of elements like that just fine

@minju25kim
Copy link
Contributor

use loop instead of string

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