2

Here, i have included a my example code. If it is one dimensional array means, i can easily insert json data's into my code. How to achieve this one with multidimensional json data with react js?

var Category = React.createClass({
  render: function() {
    return (
        <div>
          {this.props.data.map(function(el,i) {
            return <div key={i}>
                <div>
                    {el.product}
                </div>
                <div>
                    {el.quantity}
                </div>
            </div>;
          })}
        </div>
    );
  }
});

var data = [
    {
        product:"a",
        quantity:28,
        sub:[
            {
                subItem:'a'
            },
            {
                subItem:'b'
            }
        ]
    },
    {
        product:"b",
        quantity:20,
        sub:[
            {
                subItem:'a'
            },
            {
                subItem:'b'
            }
        ]
    }
];


React.render(<Category data={data}/>, document.body);

1 Answer 1

2

You can create component for sub categories like this,

var SubCategory = React.createClass({
  render: function () {
    var list = this.props.data.map(function(el, i) {
      return <li key={i}>{ el.subItem }</li>;   
    });

    return <ul>{ list }</ul>;
  }
});

and use it in Category component

{this.props.data.map(function(el,i) {
  return <div key={i}>
    <div>{el.product}</div>
    <div>{el.quantity}</div>
    <SubCategory data={ el.sub } />
  </div>;
})}

Example

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your quick response. And also i need one more help. How to read data from web servers?
@Sathya add ajax call to componentDidMound handler where you need change state with new data - like this jsfiddle.net/69z2wepo/22731. only replace in this example ajax call to you method that does ajax call - like in jquery $.ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.