简体   繁体   English

如何在 React 中从非 React 库中渲染 div 对象?

[英]How do you render div objects from non-React libraries in React?

I found a library I want to use in my project, but it's a plain JavaScript library and doesn't know anything about React.我找到了一个我想在我的项目中使用的库,但它是一个普通的 JavaScript 库,并且对 React 一无所知。

https://www.npmjs.com/package/json-formatter-js https://www.npmjs.com/package/json-formatter-js

Is it possible to use this in my React project?是否可以在我的 React 项目中使用它? I tried this, but the render crashes.我试过这个,但是渲染崩溃了。

class JSONView extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        const formatter = new JSONFormatter(this.props.data);
        const Rendered = formatter.render();

        return (
            <div>
                <Rendered />
            </div>
        );
    }
}

The error I get is this.我得到的错误是这个。

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

The typeof Rendered is object . typeof Rendered的类型是object

Try this尝试这个

class JSONView extends React.Component<any, any> {
    constructor(props) {
        super(props);
    }

    public refs:{
        JsonDiv: HTMLDivElement;
    };

    componentDidMount(){
        const formatter = new JSONFormatter(this.props.data);
        this.refs.JsonDiv.appendChild(formatter.render());
    }

    render() {
        return (
            <div ref='JsonDiv'>
            </div>
        );
    }
}

or this one或者这个

class JSONView extends React.Component<any, any> {
    constructor(props) {
        super(props);
    }

    render() {
        const formatter = new JSONFormatter(this.props.data);
        return (
            <div dangerouslySetInnerHTML={formatter.render()}>
            </div>
        );
    }
}

edit编辑

try尝试

{ Rendered }

there might be warning if it ships class with html tags.如果它运送带有 html 标签的 class 可能会有警告。

between curly braces {} and it'll work (99 %) and if it doesn't then continue reading.在花括号 {} 之间,它会工作(99 %),如果没有,则继续阅读。


you can render if it returns string.如果它返回字符串,您可以渲染。

currently it might b returning html object which is incompatible with react dom render.目前它可能会返回与反应 dom 渲染不兼容的 html object。

if it contains class property then it'll cause problem even if it's in string format.如果它包含 class 属性,那么即使它是字符串格式也会导致问题。

you can use react-html-parser to render html strings in react.你可以使用react-html-parser在 react 中渲染 html 字符串。 reference参考

const Rendered = formatter.render().outerHTML   // this will give you html string instead of html object

now use react-html-parser if you don't wanna manage class attribute name conflict as react accepts className如果你不想管理 class 属性名称冲突,现在使用react-html-parser ,因为 react 接受 className

import ReactHtmlParser from 'react-html-parser';


class JSONView extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        const formatter = new JSONFormatter(this.props.data);
        const Rendered = formatter.render().outerHTML;

        return (
            <div>
               { ReactHtmlParser(Rendered) }
            </div>
        );
    }
}

Looks like the library returns HTMLDOMElement from function formatter.render() .看起来该库从 function formatter.render()返回HTMLDOMElement

So you can't use JSX syntax to render it.所以你不能使用 JSX 语法来渲染它。

While I agree that the other answers can work, I would prefer using a dom ref and appending the returned HTMLDOMElement from formatter.render() in componentDidMount lifecycle method.虽然我同意其他答案可以工作,但我更喜欢使用 dom ref并将从formatter.render()返回的HTMLDOMElement附加到componentDidMount生命周期方法中。

import React, { Component } from 'react';
import { render } from 'react-dom';
import JSONFormatter from "json-formatter-js";
import Hello from './Hello';
import './style.css';

class JSONView extends Component {
    constructor(props) {
        super(props);
    }

    ref = null;

    componentDidMount(){
      const formatter = new JSONFormatter(this.props.data);
      const Rendered = formatter.render();
      this.ref.appendChild(Rendered);
    }

    render() {     
        return (
            <div ref={e => this.ref = e}>
            </div>
        );
    }
}

render(<JSONView data={{Hello: {Hello:  "World"}}} />, document.getElementById('root'));

Working Demo You can drill down to the object.工作演示您可以深入了解 object。

In the other approach which makes use of dangerouslySetInnerHTML or just rendering the string content , you have the risk of losing DOM events.在另一种使用dangerouslySetInnerHTML仅渲染字符串内容的方法中,您有丢失 DOM 事件的风险。

import React, { Component } from 'react';
import { render } from 'react-dom';
import JSONFormatter from "json-formatter-js";
import Hello from './Hello';

class JSONView extends Component {
    constructor(props) {
        super(props);
    }

    ref = null;

    render() {     
        const formatter = new JSONFormatter(this.props.data);
          console.log(formatter);
          const Rendered = formatter.render();
          console.log(Rendered.innerHTML);

          return (
            <div>
                <div dangerouslySetInnerHTML={{__html:Rendered.innerHTML}} />
            </div>
          );
    }
}

render(<JSONView data={{Hello: {Hello:  "World"}}} />, document.getElementById('root'));

Working Link You cannot drill down to the object.工作链接您无法深入了解 object。

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

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