Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用useEffect加载脚本_Javascript_Reactjs_Use Effect - Fatal编程技术网

Javascript 使用useEffect加载脚本

Javascript 使用useEffect加载脚本,javascript,reactjs,use-effect,Javascript,Reactjs,Use Effect,我正在使用sdk,根据文档,我必须加载脚本: 我的sdk代码 我的sdk代码 我的sdk代码 .. .. 您需要在React组件中动态添加脚本标记。最好的方法是使用上下文API。它将提供一个加载脚本和启动FB SDK的单点,并可以让每个孩子知道何时加载脚本和SDK准备好使用 FbSdkScript.js 所有这些都集中在一个组件中,其中上下文提供程序是共享对话框按钮的父级。您只需传递appId即可使FBSDKScript提供程序组件正常工作。并将href添加到按钮中,告诉FB.ui要引用的hr

我正在使用sdk,根据文档,我必须加载脚本:

我的sdk代码 我的sdk代码 我的sdk代码 .. ..
您需要在React组件中动态添加脚本标记。最好的方法是使用上下文API。它将提供一个加载脚本和启动FB SDK的单点,并可以让每个孩子知道何时加载脚本和SDK准备好使用

FbSdkScript.js

所有这些都集中在一个组件中,其中上下文提供程序是共享对话框按钮的父级。您只需传递appId即可使FBSDKScript提供程序组件正常工作。并将href添加到按钮中,告诉FB.ui要引用的href

App.js


可能有两种方式。愿意分享这些文件吗?我们可以帮你解决吗?@Emiel Zuurbier@Emiel Zuurbier,你能帮忙吗?我想实现ShareDialogIt,如果您可以共享您实际尝试构建它的代码,这将非常有帮助。
import React, { 
  createContext, 
  useContext, 
  useState, 
  useEffect 
} from 'react'

// Context.
export const FbSdkScriptContext = createContext()

// Create a custom hook to use the context.
export const useFbSdkScriptContext = () => useContext(FbSdkScriptContext)

// Provider of context.
const FbSdkScriptProvider = ({
  appId,
  autoLogAppEvents = true,
  xfbml = true,
  version = 'v8.0',
  children 
}) => {
  const [hasLoaded, setHasLoaded] = useState(false)
  const [isReady, setIsReady] = useState(false)

  /**
   * Extra security measure to check if the script has
   * already been included in the DOM
   */
  const scriptAlreadyExists = () => 
    document.querySelector('script#fb-sdk') !== null

  /**
   * Append the script to the document.
   * Whenever the script has been loaded it will
   * set the isLoaded state to true.
   */
  const appendSdkScript = () => {
    const script = document.createElement('script')
    script.id = 'fb-sdk'
    script.src = 'https://connect.facebook.net/en_US/sdk.js'
    script.async = true
    script.defer = true
    script.crossOrigin = 'anonymous'
    script.onload = () => setHasLoaded(true)
    document.body.append(script)
  };
  
  /**
   * Runs first time when component is mounted
   * and adds the script to the document.
   */
  useEffect(() => {
    if (!scriptAlreadyExists()) {
      appendSdkScript()
    }
  }, []);

  /**
   * Whenever the script has loaded initialize the
   * FB SDK with the init method. This will then set
   * the isReady state to true and passes that
   * through the context to the consumers.
   */
  useEffect(() => {
    if (hasLoaded === true) {
      FB.init({
        appId,
        autoLogAppEvents,
        xfbml,
        version 
      })
      setIsReady(true)
    }
  }, [hasLoaded])

  return (
    <FbSdkScriptContext.Provider value={{ isReady, hasLoaded }}>
      {children}
    </FbSdkScriptContext.Provider>
  )
}

export default FbSdkScriptProvider
import React, { useEffect } from 'react'
import { useFbSdkScriptContext } from './FbSdkScript'

/**
 * This is the button that will trigger the dialog.
 * It uses the context created in the previous snippet to
 * know when the script has loaded and the API is ready
 * to use. 
 */
const FbShareDialog = ({ method = 'share', href }) => {
  const { isReady } = useFbSdkScriptContext()

  /**
   * Open share dialog when the button is clicked.
   * This will only be available when the isReady
   * state is true.
   */
  const handleClick = () => {
    FB.ui({
      method,
      href,
    }, response => {
      console.log(response)
    })
  }

  /**
   * If FB SDK is not yet ready, don't render the button.
   */
  if (!isReady) {
    return null
  }

  /**
   * Otherwise do render the button and set an onClick
   * event listener which triggers the dialog.
   */
  return (
    <button onClick={handleClick}>Share me</button>
  )
}

export default FbShareDialog
import React from 'react'

import FbSdkScriptProvider from './FbSdkScript'
import FbShareDialog from './FbShareDialog'

const App = () => (
  <FbSdkScriptProvider appId={'your-app-id'}>
    <FbShareDialog href={'https://developers.facebook.com/docs/'} />
  <FbSdkScriptProvider/>
)