question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

example for react use

See original GitHub issue

hi @jdanyow,

Your work is awesome ! Just want to contribute this example made with JSX (react) cause it took me some time especially with the custom attributes for script tag.

import React, {Component} from "react";

export default class Comments extends Component {

  componentDidMount () {
      let script = document.createElement("script");
      let anchor = document.getElementById("inject-comments-for-uterances");
      script.setAttribute("src", "https://utteranc.es/client.js");
      script.setAttribute("crossorigin","anonymous");
      script.setAttribute("async", true);
      script.setAttribute("repo", "[ENTER REPO HERE]");
      script.setAttribute("issue-term", "pathname");
      script.setAttribute( "theme", "github-light");
      anchor.appendChild(script);
  }

  render() {
    return (
        <div id="inject-comments-for-uterances"></div>
    );
  }
}

Might be useful. And of course I’m gonna use your tool ! (I use gatsby js)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:37
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
osdiabcommented, Nov 7, 2019

For a more concise approach to it, this is how I just did it on my repo:

export const UtterancesComments: React.FC = () => (
  <section
    ref={elem => {
      if (!elem) {
        return;
      }
      const scriptElem = document.createElement("script");
      scriptElem.src = "https://utteranc.es/client.js";
      scriptElem.async = true;
      scriptElem.crossOrigin = "anonymous";
      scriptElem.setAttribute("repo", "osdiab/osdiab.github.io");
      scriptElem.setAttribute("issue-term", "pathname");
      scriptElem.setAttribute("label", "blog-comment");
      scriptElem.setAttribute("theme", "github-light");
      elem.appendChild(scriptElem);
    }}
  />
);

This should be safe - you get the DOM elem, this gets invoked once on mount, and once with null on unmount.

11reactions
PsyGikcommented, May 24, 2021

Using a hook:

import { useEffect, useState } from 'react';

export const useUtterances = (commentNodeId) => {
	useEffect(() => {
                 const scriptParentNode = document.getElementById(commentNodeId);
		if (!scriptParentNode) return;
		// docs - https://utteranc.es/
		const script = document.createElement('script');
		script.src = 'https://utteranc.es/client.js';
		script.async = true;
		script.setAttribute('repo', ${YOUR_REPO_NAME});
		script.setAttribute('issue-term', 'pathname');
		script.setAttribute('label', 'comment :speech_balloon:');
		script.setAttribute('theme', 'photon-dark');
		script.setAttribute('crossorigin', 'anonymous');

		
		scriptParentNode.appendChild(script);

		return () => {
			// cleanup - remove the older script with previous theme
			scriptParentNode.removeChild(scriptParentNode.firstChild);
		};
	}, [commentNodeId]);
};

Usage:

import React from 'react';
import { useUtterances } from '../../hooks/useUtterances';

const commentNodeId = 'comments';

const Comments = () => {
	useUtterances(commentNodeId);
	return <div id={commentNodeId} />;
};

export default Comments;

I’ve written an article which also shows how to lazy load, so that the comments show up when user reaches to the end of the page, (or wherever your comments are on the page), using Intersection Observer API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing Hooks - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This new...
Read more >
react-use examples - CodeSandbox
Learn how to use react-use by viewing and forking example apps that make use of react-use on CodeSandbox. ; downshift-examplesExamples of using downshift....
Read more >
React.js Examples
A nice collection of often useful examples done in React.js.
Read more >
React Tutorial - W3Schools
React is used to build single-page applications. React allows us to create reusable UI components. Start learning React now ❯. Learning by Examples....
Read more >
The Guide to Learning React Hooks (Examples & Tutorials)
Let's learn what it takes to create a custom React Hook as well as all the rules we must keep in mind when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found