Utilizing DOMPurify.sanitize()
within dangerouslySetInnerHTML={{}}
to render the innerHtml
retrieved from the database. Initially, I'm employing getServersideProps()
alongside next-redux-wrapper
for this specific page.
Installed dompurify using: npm i -S dompurify
, current version being: "^2.2.6".
Snippet of my code:
import DOMPurify from 'dompurify';
import { useSelector } from 'react-redux';
import { END } from 'redux-saga';
import wrapper from '../redux/storeSetup';
const EmployeePage = () => {
const blog = useSelector(state => state.data);
const html_body = blog[0].body_html;
const clean = DOMPurify.sanitize(html_body);
return(
<div className="mainContainer">
<div dangerouslySetInnerHTML ={{ __html: clean }} />
<ul>
{blog.map((item) => (
<li key={item.author}>
<span>{item.author}</span><br/>
<span>{item.title}</span>
<h4>{item.body_delta}</h4>
<p>{item.body_html}</p>
<p>{item.created_on}</p>
</li>
))}
</ul>
</div>
);
}
export const getServerSideProps = wrapper.getServerSideProps( async ({store}) => {
store.dispatch({type: GET_DATA});
store.dispatch(END);
await store.sagaTask.toPromise();
});
export default EmployeePage;
However, upon running npm run dev
, an error is thrown:
TypeError: dompurify__WEBPACK_IMPORTED_MODULE_1___default.a.sanitize is not a function
.
What seems to be the issue here? Tried simplifying the code but encountered the same error consistently! What am I overlooking?