DEV Community

Mohammad Shahbaz Alam
Mohammad Shahbaz Alam

Posted on

Discourse thread integration on a React Website

Discourse is an open-source civilised discussion forum and used by many Developer communities. Mozilla, Magic, Netlify, Auth0, MongoDB to name a few.

This is a simple walkthrough integration of a discourse thread in a React application.

If you are an admin or a moderator of a discourse forum, I am pretty sure you have integrated the thread post onto your website's blog, guides, or product announcements page.

For others, who are not, discourse gives the below snippet:

<div id='discourse-comments'></div>

<script type="text/javascript">
  DiscourseEmbed = { discourseUrl: 'https://community.discourse.org/',
                     discourseEmbedUrl: 'REPLACE_ME' };

  (function() {
    var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true;
    d.src = DiscourseEmbed.discourseUrl + 'javascripts/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);
  })();
</script>
Enter fullscreen mode Exit fullscreen mode

This is straightforward, just paste the above snippet to the page where you want to integrate discourse thread comments.

React

Create a discourse-forum.js file and paste the below code

import { useEffect } from 'react';

export default function DiscourseForum() {
  useEffect(() => {
    window.DiscourseEmbed = {
      discourseUrl: 'https://community.discourse.org/',
      topicId: 123,
    };

    const d = document.createElement('script');
    d.type = 'text/javascript';
    d.async = true;
    d.src = window.DiscourseEmbed.discourseUrl + 'javascripts/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);
  }, []);

  return (
    <div>
      <div id="discourse-comments"></div>
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

Replace discourseUrl with your community forum URL and topicId with the thread id from your community forum.

And make sure to go to the admin settings and click on Customise>Embedding>+Add Host and add https://www.website.com as the host.

Using DiscourseForum()

Import DiscourseForum to the page where you want to integrate the thread comments.

import DiscourseForum from '../link/to/the/file/discourse-forum.js';
Enter fullscreen mode Exit fullscreen mode

And use like this in your page

<DiscourseForum />
Enter fullscreen mode Exit fullscreen mode

You can further customise this as per your need.

Cheers!

Top comments (5)

Collapse
 
hakey1972 profile image
hakey1972

When it comes to choosing the perfect box sleeves, A comprehesive guid to selecting ideal box sleeves, box sleeves becomes indispensable. Box sleeves play a crucial role in packaging, offering both protection and aesthetic appeal. The ideal sleeves should align with the dimensions of the box, ensuring a snug fit. Material selection is vital, considering durability and visual impact. Whether for product presentation or transportation, a well-informed choice in box sleeves can enhance brand image and safeguard the contents within. This guide navigates through key considerations for selecting the optimal box sleeves for various purposes.

Collapse
 
mharis111 profile image
mharis111

Hi, nice post. I am showing the comments in my reactjs applications. But following the steps which you mentioned, it just shows the comments of the main thread. The first comment of the topic is not shown on the page where I integrated it. Do you have any idea about this issue?

Collapse
 
shahbaz17 profile image
Mohammad Shahbaz Alam

Hey @mharis111

Glad you liked it. Did you mean the previous comments or the first comment you post?

To display the comments, you need to make a comment on your created topic.

Can you share your live url where it's implemented?

Collapse
 
dandv profile image
Dan Dascalescu

How does the React version handle the discourseEmbedUrl?

Collapse
 
jaypaltagline profile image
jaypal-tagline

@shahbaz17
I try to above step it's worked on my pc.
but when I try to login then I got the cookies-related error.
Already my browser cookies setting was on.

Image description