Bra*_*don 68 javascript json flux reactjs
我正在使用ReactJS,我的应用程序的一部分需要相当印刷的JSON.
我得到一些JSON,如:{ "foo": 1, "bar": 2 },如果我JSON.stringify(obj, null, 4)在浏览器控制台中运行它,它很漂亮,但是当我在这个反应片段中使用它时:
render: function() {
var json = this.getStateFromFlux().json;
return (
<div>
<JsonSubmitter onSubmit={this.onSubmit} />
{ JSON.stringify(json, null, 2) }
</div>
);
},
Run Code Online (Sandbox Code Playgroud)
它呈现看起来像JSON "{ \"foo\" : 2, \"bar\": 2}\n".
如何正确解释这些字符?{
Wir*_*rie 151
您需要BR在结果字符串中正确插入标记,或者使用PRE标记以便stringify保留格式:
var data = { a: 1, b: 2 };
var Hello = React.createClass({
render: function() {
return <div><pre>{JSON.stringify(data, null, 2) }</pre></div>;
}
});
React.render(<Hello />, document.getElementById('container'));
Run Code Online (Sandbox Code Playgroud)
工作实例.
class PrettyPrintJson extends React.Component {
render() {
// data could be a prop for example
// const { data } = this.props;
return (<div><pre>{JSON.stringify(data, null, 2) }</pre></div>);
}
}
ReactDOM.render(<PrettyPrintJson/>, document.getElementById('container'));
Run Code Online (Sandbox Code Playgroud)

jas*_*ard 53
<pre>{JSON.stringify(data, null, 2)}</pre>
Run Code Online (Sandbox Code Playgroud)
Chr*_*ris 19
只是为了扩展WiredPrairie的答案,一个可以打开和关闭的迷你组件.
可以像:
<Pretty data={this.state.data}/>
Run Code Online (Sandbox Code Playgroud)
export default React.createClass({
style: {
backgroundColor: '#1f4662',
color: '#fff',
fontSize: '12px',
},
headerStyle: {
backgroundColor: '#193549',
padding: '5px 10px',
fontFamily: 'monospace',
color: '#ffc600',
},
preStyle: {
display: 'block',
padding: '10px 30px',
margin: '0',
overflow: 'scroll',
},
getInitialState() {
return {
show: true,
};
},
toggle() {
this.setState({
show: !this.state.show,
});
},
render() {
return (
<div style={this.style}>
<div style={this.headerStyle} onClick={ this.toggle }>
<strong>Pretty Debug</strong>
</div>
{( this.state.show ?
<pre style={this.preStyle}>
{JSON.stringify(this.props.data, null, 2) }
</pre> : false )}
</div>
);
}
});
Run Code Online (Sandbox Code Playgroud)
一种更现代的方法(现在createClass即将推出)
import styles from './DebugPrint.css'
import autoBind from 'react-autobind'
import classNames from 'classnames'
import React from 'react'
export default class DebugPrint extends React.PureComponent {
constructor(props) {
super(props)
autoBind(this)
this.state = {
show: false,
}
}
toggle() {
this.setState({
show: !this.state.show,
});
}
render() {
return (
<div style={styles.root}>
<div style={styles.header} onClick={this.toggle}>
<strong>Debug</strong>
</div>
{this.state.show
? (
<pre style={styles.pre}>
{JSON.stringify(this.props.data, null, 2) }
</pre>
)
: null
}
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
和你的风格文件
.root {backgroundColor:'#1f4662'; 颜色:'#ff'; fontSize:'12px'; }
.header {backgroundColor:'#193549'; 填充:'5px 10px'; fontFamily:'monospace'; 颜色:'#ffc600'; }
.pre {display:'block'; 填充:'10px 30px'; 保证金:'0'; 溢出:'滚动'; }
' react-json-view '提供解决方案渲染json字符串。
import ReactJson from 'react-json-view';
<ReactJson src={my_important_json} theme="monokai" />
Run Code Online (Sandbox Code Playgroud)
const getJsonIndented = (obj) => JSON.stringify(newObj, null, 4).replace(/["{[,\}\]]/g, "")
const JSONDisplayer = ({children}) => (
<div>
<pre>{getJsonIndented(children)}</pre>
</div>
)
Run Code Online (Sandbox Code Playgroud)
然后你可以轻松使用它:
const Demo = (props) => {
....
return <JSONDisplayer>{someObj}<JSONDisplayer>
}
Run Code Online (Sandbox Code Playgroud)