简体   繁体   English

React:为什么这个 React 组件不渲染?

[英]React: Why won't this React Component render?

I'm in the process of writing a desktop application in React using Electron and Meteor.js我正在使用 Electron 和 Meteor.js 在 React 中编写桌面应用程序

I have the following React Component class:我有以下反应组件 class:

import React from "react"

export class MemoryMap extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            memory : [],
            mem_max : 0xFFFF,
        }

        this.readByte = function(byte){
            return this.state.memory[byte];
        };

        this.writeByte = function(mem_addr, byte){
            if(mem_addr >= 0 && mem_addr < this.state.mem_max) {
                this.state.memory[mem_addr] = byte;
            }
        };

        for(let i = 0; i < 10000; i++){
            this.state.memory[i] = 0x0000;
        }



    }

    render(){
        return(
            <div>
                <h1>{this.state.memory[0x0000]}</h1>
            </div>
        );
    }
}

export const MemMap = new MemoryMap();

I attempt to render this class in Main.jsx as such:我尝试在 Main.jsx 中呈现这个 class :

import React from 'react';
import { Meteor } from 'meteor/meteor';
import { render } from 'react-dom';
import {MemMap} from "./CPU_Elements/MemoryMap";


Meteor.startup(() => {

  render(<MemMap/>, document.getElementById("react-target"));
  Desktop.send("desktop", "init");
});

When called this way, the program crashes on this line.当以这种方式调用时,程序会在这一行崩溃。 The Desktop.send function is never called. Desktop.send function 永远不会被调用。

If I re-write MemoryMap as such, where the render function becomes a class method:如果我这样重写 MemoryMap,则渲染 function 变为 class 方法:

import React from "react"

export class MemoryMap extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            memory : [],
            mem_max : 0xFFFF,
        }

        this.readByte = function(byte){
            return this.state.memory[byte];
        };

        this.writeByte = function(mem_addr, byte){
            if(mem_addr >= 0 && mem_addr < this.state.mem_max) {
                this.state.memory[mem_addr] = byte;
            }
        };

        for(let i = 0; i < 10000; i++){
            this.state.memory[i] = 0x0000;
        }

        this.render = function(){
            return(
                <div>
                    <h1>{this.state.memory[0x0000]}</h1>
                </div>
            );
        }

    }


}

export const MemMap = new MemoryMap();

And the main.jsx file is re-written to call that method:并且 main.jsx 文件被重写以调用该方法:

import React from 'react';
import { Meteor } from 'meteor/meteor';
import { render } from 'react-dom';
import {MemMap} from "./CPU_Elements/MemoryMap";


Meteor.startup(() => {

  render(MemMap.render(), document.getElementById("react-target"));
  Desktop.send("desktop", "init");
});

The element renders just fine.元素渲染得很好。

Why is this?为什么是这样? Why can't I use the HTML tag formatting, as shown in React's tutorials?为什么我不能使用 HTML 标签格式,如 React 的教程中所示?

change this:改变这个:

export const MemMap = new MemoryMap();

to:至:

export const MemMap = MemoryMap;

Since you should export the component defination, not creating an instance of it and exporting it.由于您应该导出组件定义,而不是创建它的实例并导出它。 (that's why obj.render() works but <obj/> doesn't.) (这就是为什么obj.render()有效但<obj/>无效的原因。)

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

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