While trying to get the h3
heading text value using react-markdown
, react web app throws following error in console. Could someone please advise, how can I get the h3 text value here ?
Uncaught TypeError: reactMarkdown.props.children.filter is not a function at getHeadingText (blogItem.js:86:1) at onClick (blogItem.js:99:1) at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
function getHeadingText(markdownString) {
if (!markdownString) {
return null;
}
const reactMarkdown = <ReactMarkdown children={markdownString} remarkPlugins={[remarkGfm]} />
console.log("Here::"+ reactMarkdown); // Debug output
const h3Headings = reactMarkdown.props.children.filter(child => child.type === 'h3');
const headingTexts = h3Headings.map(heading => heading.props.children);
return headingTexts[0];
}
const Columns = () =>
<div className='blogItems'>
<div className='row'>
<div className='blogArea'>
{!results.length && (<div className="noSearchData"><Wave text="Sorry, we couldn't find any results..!" /></div>)}
{
results?.slice(0, loadBlogs).map (({id, blogdetails, views, createdAt }) => (
<a key={id}
onClick={
() =>
navigate(`/blogDetails/${getHeadingText(blogdetails)}`, {
state: { id, blogdetails, views, createdAt}
})
}
>
<div className='dataArea' onClick={() => getClickCount(id)} >
<ReactMarkdown children={blogdetails} className='dataDate renderElipsis tags readmoreLink views' remarkPlugins={[remarkGfm]}>
</ReactMarkdown>
<div className='blogDate'>
{moment(createdAt).format('DD-MMM-YYYY')}
<a className='moreLink'>
Read more →
</a>
</div>
</div>
</a>
))}
</div>
</div>
</div>
Please note: react-markdown string looks like as below:
"
##### 27 Feb 2023
###### unix
### Unix commands relevant
Best heading added here.Print the whole text here."