Javascript React Useffect附加脚本未触发

Javascript React Useffect附加脚本未触发,javascript,reactjs,react-hooks,use-effect,Javascript,Reactjs,React Hooks,Use Effect,我正在尝试加载一个脚本,一旦被触发,它将生成一个iframe和一个weather小部件。我可以正确地附加脚本,但它不会被调用,也不会生成iframe 我已经尝试直接在渲染中添加脚本,它将短暂加载iframe,然后切换回脚本标记 const City = ({ content, schema }) => { useEffect(() => { let script = document.createElement("script"); script.src =

我正在尝试加载一个脚本,一旦被触发,它将生成一个iframe和一个weather小部件。我可以正确地附加脚本,但它不会被调用,也不会生成iframe

我已经尝试直接在渲染中添加脚本,它将短暂加载iframe,然后切换回脚本标记

const City = ({ content, schema }) => {
  useEffect(() => {
    let script = document.createElement("script");
    script.src =
  "https://darksky.net/widget/graph-bar/32.7174,-117.1628/us12/en.js?width=100%&height=400&title=Full Forecast&textColor=333333&bgColor=FFFFFF&transparency=false&skyColor=undefined&fontFamily=Default&customFont=&units=us&timeColor=333333&tempColor=ff8200&currentDetailsOption=true";
    script.async = true;

    document.getElementById("widget").appendChild(script);
  }, [])

  return (<div id="widget"></div>);
};

export default City;
constcity=({content,schema})=>{
useffect(()=>{
让script=document.createElement(“脚本”);
script.src=
"https://darksky.net/widget/graph-bar/32.7174,-117.1628/us12/en.js?宽度=100%,高度=400&title=Full-Forecast&textColor=333333&bgColor=ffffffff&transparency=false&skyColor=undefined&fontffamily=Default&customFont=&units=us&timeColor=333333&tempColor=ff8200¤tdailsoption=true”;
script.async=true;
document.getElementById(“小部件”).appendChild(脚本);
}, [])
返回();
};
导出默认城市;

注意控制台中显示的警告:

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

请参见

既然要添加到widget div,为什么不在return中写入script标记呢?@Chris我试过了,它会触发,但会闪烁并返回脚本