如何在文本区域内呈现漂亮的json数据React js?

时间:2021-09-25 21:44:34

I am new to react js.I have a problem in rendering the pretty json data inside textarea.I don't know Which part is wrong I want my prettyjson to render inside textarea Like this

我是新来的反应js.I有一个问题在textarea里面渲染漂亮的json数据。我不知道哪个部分是错的我希望我的prettyjson在textarea里面渲染像这样

"email":"xxxx@x.com",
"email":"yyyy@y.com",
.....

This is my code But I am getting Nothing inside my textarea

这是我的代码但我在textarea中没有任何东西

/**
 * Created by arfo on 6/26/2016.
 */
var React =require('react');
var api = require('../utils');


var Bulkmail = React.createClass({
    getInitialState:function () {
      return{
          default:10,
          data:[],
          color:'#58FA58'

      }
    },
    componentDidMount:function () {
        api.getemail(this.state.default).then(function (response) {
           this.setState({
               data:response

           })
        }.bind(this))
    },
    onSubmit:function (e) {
      e.preventDefault();
      console.log(this.refs.text.value.trim());


    },

    onChange:function (e) {
     e.preventDefault();
        //console.log(this.refs.text.value.trim())
        var data = this.refs.text.value.trim();
        if(isNaN(data)){
            this.setState({
                color:'#FE2E2E'
            })
        }else{
            this.setState({
                color:'#58FA58'
            })
        }
    },
    render:function () {
        console.log(this.state.data);
        var results = this.state.data;
        return(
           <div className="bodybox">
               <div className="box">
                  <div className="upc">
                      <p>Generate Bulk Email</p>
                      <form onSubmit={this.onSubmit}>
                      <input onChange={this.onChange} type="text" style={{border:'1px solid '+this.state.color}}  ref="text" defaultValue={this.state.default} placeholder="Enter Number"/>
                       <button>Get Data</button>
                      </form>
                      <div className="result">
                          <ul>
                              {this.state.data.map(function (data) {
                                  return  <li key={data.id}>{data.email}</li>
                              })}

                          </ul>
                      </div>

                  </div>
                   <div className="tdown">

                       <p>Json Format</p>

                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       <textarea defaultValue={this.state.data.map(function(data) {
                           return JSON.stringify(data.email)
                       })}  >
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       </textarea>
                   </div>
               </div>
           </div>
        )
    }
});

module.exports = Bulkmail ;

2 个解决方案

#1


2  

 <textarea value={this.state.data.map(e=>JSON.stringify(e))}  defaultValue="val" />

result {"email":"some@mail"},{"email":"some@mail"},{"email":"some@mail"}


let value = this.state.data.map(e=>JSON.stringify(e).replace(/{|}/g,''));

<textarea value={value}  defaultValue="val" />

result "email" : "xx@yy.y", "email" : "some@mail", "email" : "some@mail"

结果“email”:“xx@yy.y”,“email”:“some @ mail”,“email”:“some @ mail”


 let value = this.state.data.map(e=>JSON.stringify(e).replace(/{|}/g,'')).join(',\n');

 <textarea value={value}  defaultValue="val" />

result "email" : "xx@yy.y", "email" : "some@mail", "email" : "some@mail"

结果“email”:“xx@yy.y”,“email”:“some @ mail”,“email”:“some @ mail”

In HTML, the value of is set via children. In React, you should use value instead.

在HTML中,值是通过子设置的。在React中,您应该使用值代替。

#2


0  

/**
 * Created by arfo on 6/26/2016.
 */
var React =require('react');
var api = require('../utils');


var Bulkmail = React.createClass({
    getInitialState:function () {
      return{
          default:10,
          data:[],
          color:'#58FA58'

      }
    },
    componentDidMount:function () {
        api.getemail(this.state.default).then(function (response) {
           this.setState({
               data:response

           })
        }.bind(this))
    },
    onSubmit:function (e) {
      e.preventDefault();
      console.log(this.refs.text.value.trim());


    },

    onChange:function (e) {
     e.preventDefault();
        //console.log(this.refs.text.value.trim())
        var data = this.refs.text.value.trim();
        if(isNaN(data)){
            this.setState({
                color:'#FE2E2E'
            })
        }else{
            this.setState({
                color:'#58FA58'
            })
        }
    },

    getEmailValue:function(){
    return this.state.data.map(function(data) {
                           return JSON.stringify(data.email)
                       }).join('\n');
},
    render:function () {
        console.log(this.state.data);
        var results = this.state.data;
        return(
           <div className="bodybox">
               <div className="box">
                  <div className="upc">
                      <p>Generate Bulk Email</p>
                      <form onSubmit={this.onSubmit}>
                      <input onChange={this.onChange} type="text" style={{border:'1px solid '+this.state.color}}  ref="text" defaultValue={this.state.default} placeholder="Enter Number"/>
                       <button>Get Data</button>
                      </form>
                      <div className="result">
                          <ul>
                              {this.state.data.map(function (data) {
                                  return  <li key={data.id}>{data.email}</li>
                              })}

                          </ul>
                      </div>

                  </div>
                   <div className="tdown">

                       <p>Json Format</p>


                       <textarea value={getEmailValue()}

                       </textarea>
                   </div>
               </div>
           </div>
        )
    }
});

#1


2  

 <textarea value={this.state.data.map(e=>JSON.stringify(e))}  defaultValue="val" />

result {"email":"some@mail"},{"email":"some@mail"},{"email":"some@mail"}


let value = this.state.data.map(e=>JSON.stringify(e).replace(/{|}/g,''));

<textarea value={value}  defaultValue="val" />

result "email" : "xx@yy.y", "email" : "some@mail", "email" : "some@mail"

结果“email”:“xx@yy.y”,“email”:“some @ mail”,“email”:“some @ mail”


 let value = this.state.data.map(e=>JSON.stringify(e).replace(/{|}/g,'')).join(',\n');

 <textarea value={value}  defaultValue="val" />

result "email" : "xx@yy.y", "email" : "some@mail", "email" : "some@mail"

结果“email”:“xx@yy.y”,“email”:“some @ mail”,“email”:“some @ mail”

In HTML, the value of is set via children. In React, you should use value instead.

在HTML中,值是通过子设置的。在React中,您应该使用值代替。

#2


0  

/**
 * Created by arfo on 6/26/2016.
 */
var React =require('react');
var api = require('../utils');


var Bulkmail = React.createClass({
    getInitialState:function () {
      return{
          default:10,
          data:[],
          color:'#58FA58'

      }
    },
    componentDidMount:function () {
        api.getemail(this.state.default).then(function (response) {
           this.setState({
               data:response

           })
        }.bind(this))
    },
    onSubmit:function (e) {
      e.preventDefault();
      console.log(this.refs.text.value.trim());


    },

    onChange:function (e) {
     e.preventDefault();
        //console.log(this.refs.text.value.trim())
        var data = this.refs.text.value.trim();
        if(isNaN(data)){
            this.setState({
                color:'#FE2E2E'
            })
        }else{
            this.setState({
                color:'#58FA58'
            })
        }
    },

    getEmailValue:function(){
    return this.state.data.map(function(data) {
                           return JSON.stringify(data.email)
                       }).join('\n');
},
    render:function () {
        console.log(this.state.data);
        var results = this.state.data;
        return(
           <div className="bodybox">
               <div className="box">
                  <div className="upc">
                      <p>Generate Bulk Email</p>
                      <form onSubmit={this.onSubmit}>
                      <input onChange={this.onChange} type="text" style={{border:'1px solid '+this.state.color}}  ref="text" defaultValue={this.state.default} placeholder="Enter Number"/>
                       <button>Get Data</button>
                      </form>
                      <div className="result">
                          <ul>
                              {this.state.data.map(function (data) {
                                  return  <li key={data.id}>{data.email}</li>
                              })}

                          </ul>
                      </div>

                  </div>
                   <div className="tdown">

                       <p>Json Format</p>


                       <textarea value={getEmailValue()}

                       </textarea>
                   </div>
               </div>
           </div>
        )
    }
});