简体   繁体   English

Html React 设置的内容显示在控制台但不显示在 DOM 中

[英]Html Content set by react showing in console but not in DOM

I am using react by adding react CDN in index.html file in my simple HTML, CSS, and JS editor according to according to this doc React , and it is working fine in my code editor's preview: Code Editor's Preview我通过在我的简单 HTML、CSS 和 JS 编辑器中的 index.html 文件中添加 React CDN 来使用 React,根据此文档React ,它在我的代码编辑器预览中运行良好: Code Editor's Preview

But on the main web page, it is not showing the react output set by root.render.但是在 web 主页面上,它没有显示由 root.render 设置的 react output。 My Webpage View我的网页视图

I want my root.render to run two times accurately and show my data as it is showing in the preview.我希望我的 root.render 准确运行两次并显示我的数据,因为它在预览中显示。 Even the data is showing in the console but not on the page like this: WebPage with console甚至数据也显示在控制台中,而不是像这样显示在页面上: WebPage with console

"like_button_container86551" is the ID of the div in which react app will render, you can access the complete code which I am using here React website file “like_button_container86551”是 react 应用程序将在其中呈现的 div 的 ID,您可以访问我在这里使用的完整代码React 网站文件

I don't get the real reason but I solved it with a trick, as data was showing in the console that means that DOM is not showing the updated data so I just assign a key to my div of the content我不明白真正的原因,但我用一个技巧解决了它,因为数据显示在控制台中,这意味着 DOM 没有显示更新的数据,所以我只是为我的内容 div 分配一个键

<span onClick={handleClickOpen} key={contentKey}>
...
</span>

and after the rendering of the component in the useEffect function I increase the key by 1 which means I refresh the DOM element and now the content is shown:在 useEffect function 中渲染组件后,我将键值增加 1,这意味着我刷新了 DOM 元素,现在显示了内容:

useEffect(() => {
   setContentKey(1 + contentKey);
}, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM