React: How to render a component twice in a page?(React:如何在页面中两次渲染组件?)
问题描述
我在 React 中有一个组件构建myComponent.js
I have a component build in ReactmyComponent.js
ReactDOM.render(
<MyComponent />,
document.getElementById('my-id')
);
在我的 Html 中,我想渲染两次,HTML 如下:
In my Html, I want to render it twice, the HTML is as following:
<div id='my-id></div>
some html
<div id='my-id></div>
我想在这个页面中反应渲染两次,但它只为第二个 div 渲染一次.反正有没有渲染两次?
I want react render twice in this page, but it only render once for the second div. Is there anyway to render it twice?
推荐答案
HTML 中的两个或多个元素不能有相同的 id.使用不同的ids
You can't have the same id for two or more elements in HTML. Use different ids
<div id='my-id-one></div>
some html
<div id='my-id-two></div>
并为每个 id 分别调用 ReactDOM.render.
and call the ReactDOM.render separately for each id.
ReactDOM.render(
<MyComponent />,
document.getElementById('my-id-one')
);
ReactDOM.render(
<MyComponent />,
document.getElementById('my-id-two')
);
这篇关于React:如何在页面中两次渲染组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:React:如何在页面中两次渲染组件?
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
