简体   繁体   English

将多个ReactJS组件安装到单个网页中

[英]Mount Multiple ReactJS Components into a single web page

I have multiple, reusable components on a single web page. 我在一个网页上有多个可重用的组件。 For example, a popup, sidebar newsletter signup and a simple carousel below the content. 例如,内容下方有一个弹出窗口,侧边栏新闻通讯注册和一个简单的轮播。

I'm getting the following error 我收到以下错误

'__reactInternalInstance$lvoo7hroqz' of null

After some research I believe this is down to having multiple calls to react, which makes sense. 经过一些研究,我认为这归因于多次调用做出反应,这是有道理的。 Every component imports React I believe this is due to the fact react adds id's to each node and it conflicts on each instance that's called. 我相信每个组件都会导入React,这是由于react在每个节点上添加了id并在每个被调用的实例上发生了冲突。

My question is how would I render multiple components on a single web page? 我的问题是如何在单个网页上呈现多个组件? when there's no parent node/container and these elements are called individually throughout the site. 当没有父节点/容器并且在整个站点中分别调用这些元素时。

Thanks 谢谢

EDIT 编辑

I have three components that look similar to below. 我有三个看起来类似于下面的组件。

import React, { Component } from 'react'

export default class test extends Component {

render(){
return (<h1>test one</h1>)
}

These three individual components appear randomly around my page. 这三个单独的组件随机出现在我的页面周围。

<div>
  <header>
    Some HTML/PHP here
    <ReactTestComponent />
  </header>

  <div>
    content here 
    <AnotherComponent />
  </div>

  <FinalComponent/>
</div>

These three components do not always appear on the same page, for instance "FinalComponent" may be missing from the next page (depending if you're on a archive page etc) so all my components need "import React from 'react'" at the top of each file. 这三个组件并不总是出现在同一页面上,例如,下一页可能会缺少“ FinalComponent”(取决于您是否在存档页面等),因此我所有的组件都需要在以下位置“从“反应”导入React”每个文件的顶部。

When I render multiple components on a single page. 当我在单个页面上呈现多个组件时。 I get the following errors. 我收到以下错误。 (Based on the amount of components rendered, if I render two components I get two of the same error) (基于渲染的组件数量,如果渲染两个组件,则会得到两个相同的错误)

Uncaught TypeError: Cannot read property '__reactInternalInstance$lvoo7hroqz' of null
at Object.getClosestInstanceFromNode (react.min.js:504)
at findParent (react.min.js:36970)
at handleTopLevelImpl (react.min.js:36999)
at ReactDefaultBatchingStrategyTransaction.perform (react.min.js:6065)
at Object.batchedUpdates (react.min.js:36768)
at Object.batchedUpdates (react.min.js:1779)
at dispatchEvent (react.min.js:37079)

OK, looking into this a little further I noticed it was a error on my part. 好吧,再仔细研究一下,我发现这是我的错误。

One of the includes was calling the same react file in addition to the footer, so I had two referenced to react.min.js causing the conflict. 其中的一个是在页脚之外调用相同的react文件,因此我引用了两个react.min.js导致冲突。

Hope this helps someone. 希望这对某人有帮助。

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

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