简体   繁体   English

React类不渲染

[英]React class not rendering

I'm trying to follow along with the reactjs tic tac toe tutorial, but when I try to render the pages and I have this code 我正在尝试遵循reactjs tic tac toe教程,但是当我尝试呈现页面并且有此代码时


    import React from 'react'
    import ReactDOM from 'react-dom'
    import Game from '../components/Game'
    document.addEventListener('DOMContentLoaded', () => {
        ReactDOM.render(
            console.log("render is working"),
            <Game />,
            document.getElementById('root'),
        );
    })

and the Game class looks like 游戏类看起来像

    import React from 'react'
    import ReactDOM from 'react-dom'
    import Board from './Board'

    class Game extends React.Component {
        render() {
            console.log("Hooray, the game is working")
            return (
                <div className="game">
                    <div className="game-board">
                        <Board />
                    </div>
                    <div className="game-info">
                        <div>{/* status */}</div>
                        <ol>{/* TODO */}</ol>
                    </div>
                </div>
            );
        }
    }

    export default Game

The 'render is working prints to the console but the console log that says "Hooray, the game is working" doesn't show up, so it seems like the Game class isn't getting rendered for some reason. “渲染器正在向控制台打印输出,但是控制台日志中没有显示“万岁,游戏正在运行”,因此似乎出于某些原因未渲染Game类。 I'm using ruby on rails with the built in webpacker to load all the javascripts 我正在使用内置webpacker的Rails在Rails上加载所有JavaScript

ReactDOM.render() takes 2 arguments: component, and mounting DOM element. ReactDOM.render()有两个参数:component和mount DOM元素。 You are passing a console.log for the first argument. 您正在为第一个参数传递console.log

Cause calling console.log evaluates to undefined you are basically doing: 导致调用console.log结果基本上是undefined

ReactDOM.render(
   undefined, // <- react component
   <Game />, // <- target 
   document.getElementById('root'), // < - useless
);

I'm surprised that ReactDOM.render is not throwing an error although you are passing completely useless arguments to it. 我很惊讶ReactDOM.render不会引发错误,尽管您正在向它传递完全无用的参数。

For one, why do you have your ReactDom.render(...) wrapped in that event listener? 首先,为什么要在事件监听器中包装ReactDom.render(...) Two, why are you passing the ReactDom.render(...) a console.log() ? 二,为什么要通过ReactDom.render(...)传递console.log() It should only take two arguments: a component, and the element it should be appended to. 它应该仅包含两个参数:一个组件以及应该附加到其上的元素。 Try removing the console.log() and if that still doesn't work, try removing the event listener that wraps your ReactDoom.render(...) . 尝试删除console.log() ,如果仍然无效,请尝试删除包装了ReactDoom.render(...)的事件侦听器。

You need to remove the console log from the ReactDOM render call. 您需要从ReactDOM渲染调用中删除控制台日志。

If I need to render in a component, I can do it as such. 如果需要在组件中渲染,则可以这样做。

<div>
  .....
  {console.log('Inline console in render method')}
</div>

Your render should be as follows: 您的渲染应如下所示:

ReactDOM.render(<Game />, document.getElementById('root);

Assuming your child component is passed as 'export default Game' and your Id of your HTML base file is 'root'. 假设您的子组件作为“导出默认游戏”传递,并且您的HTML基本文件的ID为“ root”。

Render only takes two arguments. 渲染仅需要两个参数。 The first being the component being passed in and the second is the target in which it should place itself. 第一个是要传入的组件,第二个是应放置其自身的目标。 You should only need to do this step at your 'index.js' file. 您只需要在“ index.js”文件中执行此步骤。 Nowhere else. 无处。

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

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