Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Comments plugin in React/Node.js app only shows on refresh

I have a container that renders a few component along a FB comment. I am on React and it looks like the facebook comment logic only works on the server side as it render only on a refresh and not click through in the application. How can I get the Facebook Comment widget to show on a non-refersh (server-side rendering)?

export default class MyComponent extends Component {
  constructor() {
    super();
  }

  componentDidMount() {
    // Facebook comment SDK
    const fbSDK = (d, s, id) => {
      let js;
      const fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s); js.id = id;
      js.src = '//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.6&appId=xxxxxxxxxx';
      fjs.parentNode.insertBefore(js, fjs);
    };
    fbSDK(document, 'script', 'facebook-jssdk');
  }

  // some more code

  render() {

    // some more code

    return (

      // some more code

      <div className={styles.fbCommentContainer}>
        <div id="fb-root"></div>
        <div className="fb-comments" data-href={'http://www.example.com' + this.props.location.pathname}
                 data-colorscheme="dark" data-width="100%" data-numposts="2" data-order-by="reverse_time"></div>
      </div>
    );
  }
}
like image 387
Sam Sedighian Avatar asked May 25 '26 01:05

Sam Sedighian


1 Answers

You add this code into where ever your component is.

componentDidMount(){
   window.FB.XFBML.parse();
}

It will re-parse the dom after mounting your component. It works for me.

like image 80
user3856437 Avatar answered May 26 '26 16:05

user3856437