简体   繁体   English

React - 错误:目标容器不是 DOM 元素

[英]React - Error: Target container is not a DOM element

I just got started using React so this is probably a very simple mistake, but here we go.我刚开始使用 React,所以这可能是一个非常简单的错误,但我们开始吧。 The following code throws me always the following message: Error: Target container is not a DOM element.以下代码总是向我抛出以下消息:错误:目标容器不是 DOM 元素。 Furthermore it indicates that there's something wrong with the last line (render).此外,它表明最后一行(渲染)有问题。 I'm working through a tutorial for LinguiJS and I use their code so I'm confused why this is not working.我正在学习 LinguiJS 的教程,我使用他们的代码,所以我很困惑为什么这不起作用。

I tried replacing render by ReactDOM.render and adapted the import consequently, but I received the same error.我尝试用ReactDOM.render替换render并因此调整了导入,但我收到了同样的错误。 Any help would be highly appreciated!任何帮助将不胜感激!

//index.js

import React from 'react'
import { render } from 'react-dom'
import Inbox from './Inbox.js'
import catalogCs from './locales/cs/messages.js'

import { I18nProvider } from '@lingui/react'

const catalogs = { cs: catalogCs };
const App = () => (
  <I18nProvider language="cs" catalogs={catalogs}>
    <Inbox />
  </I18nProvider>
)

render(<App />, document.getElementById('app'))

Second file:第二个文件:

//Inbox.js
import React from 'react'
import { Trans } from '@lingui/macro'
import {Link} from 'react-router-dom';


const Inbox = ({ messages, markAsRead, user }) => {
   const messagesCount = messages.length
   const { name, lastLogin } = user

   return (
      <div>
        <h1><Trans>Message Inbox</Trans></h1>

        <p>
          <Trans>
          See all <Link to="/unread">unread messages</Link>{" or "}
          <a onClick={markAsRead}>mark them</a> as read.
          </Trans>
        </p>

        <p>
          {
            messagesCount === 1
              ? `There's ${messagesCount} message in your inbox.`
              : `There're ${messagesCount} messages in your inbox.`
          }
        </p>

        <footer>
          Last login on {lastLogin}.
        </footer>
      </div>
   )
}

export default Inbox;

I found the answer to my question.我找到了我的问题的答案。 It's rather simple;这相当简单; the in index.html was id=root , thus I only had to change render(<App />, document.getElementById('app')) to render(<App />, document.getElementById('root')) index.html 中的id=root ,因此我只需要将render(<App />, document.getElementById('app'))更改为render(<App />, document.getElementById('root'))

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

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