简体   繁体   English

如何打印/记录reactjs渲染的dom?

[英]How to print/log reactjs rendered dom?

I'm creating a component with ReactJS. 我正在用ReactJS创建一个组件。

I would see the html react renders with my console, so for example: 我会在控制台上看到html react渲染,因此例如:

 import React, { PropTypes } from 'react';
 import ReactDOM from 'react-dom';

 class CustomComponent extends React.Component {


   render() { return (<div>Custom</div>); }
 }

And instead of: 而不是:

ReactDOM.render(<CustomComponent />, document.getElementById('app'));

I would do something like: 我会做类似的事情:

console.log(<CustomComponent />); 

but when in my console I call: 但是在控制台中时,我会调用:

babel-node myfile.js

I get: 我得到:

在此处输入图片说明

How can I print/log the effective html tags that react renders? 我该如何打印/记录有效的html标签以响应渲染?

thanks. 谢谢。 bye. 再见。

Use https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostring 使用https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostring

Now you're just printing the virtual dom representation which need to be transformed to string. 现在,您只是打印需要转换为字符串的虚拟dom表示形式。

you need to use string renderToStaticMarkup(ReactElement element) 您需要使用string renderToStaticMarkup(ReactElement element)

it will return you the StaticMarkup 它将返回您StaticMarkup

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

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