React change class to className

Viewed 41

I am fetching data from a WordPress rest API and it is returning some rendered HTML as shown below:

rendered    "<div class=\"mt-4\">\n\t\t<h3 class=\"pb-2 mb-4 mt-5\">Recent Posts</h3>\n\t\t<ul>\n\t\t\t\t\t\t\t\t\t\t\t<li>\n\t\t\t\t\t<a href=\"https://yellopals.com/headlesscms/wordpress/blog/where-does-it-come-from/\">Where does it come from?</a>\n\t\t\t\t\t\t\t\t\t\t\t<span class=\"post-date\">October 13, 2021</span>\n\t\t\t\t\t\t\t\t\t</li>\n\t\t\t\t\t\t\t\t\t\t\t<li>\n\t\t\t\t\t<a href=\"https://yellopals.com/headlesscms/wordpress/blog/new-post-for-test/\">New post for test</a>\n\t\t\t\t\t\t\t\t\t\t\t<span class=\"post-date\">October 13, 2021</span>\n\t\t\t\t\t\t\t\t\t</li>\n\t\t\t\t\t\t\t\t\t\t\t<li>\n\t\t\t\t\t<a href=\"https://yellopals.com/headlesscms/wordpress/blog/develop-wordpress-theme-bootstrap-to-wordpress-step2-custom-theme-integration/\">Develop WordPress Theme (Bootstrap to WordPress) &#8211; Step2 &#8211; Custom Theme Integration</a>\n\t\t\t\t\t\t\t\t\t\t\t<span class=\"post-date\">October 18, 2020</span>\n\t\t\t\t\t\t\t\t\t</li>\n\t\t\t\t\t\t\t\t\t\t\t<li>\n\t\t\t\t\t<a href=\"https://yellopals.com/headlesscms/wordpress/blog/develop-wordpress-theme-bootstrap-to-wordpress/\">Develop WordPress Theme (Bootstrap to WordPress)</a>\n\t\t\t\t\t\t\t\t\t\t\t<span class=\"post-date\">October 4, 2020</span>\n\t\t\t\t\t\t\t\t\t</li>\n\t\t\t\t\t\t\t\t\t\t\t<li>\n\t\t\t\t\t<a href=\"https://yellopals.com/headlesscms/wordpress/blog/10-html-tags-that-you-dont-use-but-are-useful/\">10 HTML tags that you don’t use but are useful</a>\n\t\t\t\t\t\t\t\t\t\t\t<span class=\"post-date\">September 20, 2020</span>\n\t\t\t\t\t\t\t\t\t</li>\n\t\t\t\t\t</ul>\n\n\t\t</div>"

and REACT in the console showing error "Warning: Invalid DOM property class. Did you mean className?"

Is there any way to change "class" attribute to "className"? Tried <div dandangerouslySetInnerHTML={{__html:data.rendered}} > but showing same error

1 Answers

Response it a string so you can use replace method to replace class to className and then render it. Like this:

response.replace(/class=/g, "className=")
Related