How to integrate the Twitter widget into Reactjs?

前端 未结 9 2414
我在风中等你
我在风中等你 2021-02-18 20:33

I want to add the Twitter widget into React, but I don\'t know where to start or how to do it. I am very new to React JS.

Here is the HTML version of the code:

9条回答
  •  猫巷女王i
    2021-02-18 21:25

    It's easy, all you need just use React's componentDidMount hook

    {createClass, DOM:{a}} = require "react"
    
    @TwitterWidget = createClass
      componentDidMount: ->
        link = do @refs.link.getDOMNode
        unless @initialized
          @initialized = yes
          js = document.createElement "script"
          js.id = "twitter-wjs"
          js.src = "//platform.twitter.com/widgets.js"
          link.parentNode.appendChild js
    
      render: ->
        {title, content, widget} = @props
        a
          ref: "link"
          className: "twitter-timeline"
          href: "https://twitter.com/#{widget.path}"
          "data-widget-id": widget.id,
          widget.title
    
    TwitterWidget
      widget:
        id: "your-widget-id"
        title: "Your Twitts"
        path: "widget/path"
    

提交回复
热议问题