I
I
Ilya Alekseev2015-11-04 13:54:06
JavaScript
Ilya Alekseev, 2015-11-04 13:54:06

How to get 1 value from an array in React?

Here I have values ​​in orders array, there are products, username and order id

var GoodsTable = React.createClass({
 getInitialState: function(){return{goods: []};
},
         render: function(){
   var itemNodes = this.state.goods.map(function(tableItem){
   return (<GoodsRow item={tableItem}/>);
      		});

then we set the data in the array and pass it to render in GoodsRow, and here's how to do it in GoodsRow if there are 3 goods, and the user needs to be written 1 time? now the code is like this
var GoodsRow = React.createClass({
 render: function(){
 	var row = (
<tr>
  <td>{this.props.item.Good_Name}</td>
  <td>{this.props.item.Login}</td>
</tr>);
   return row;
 }
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Zuev, 2015-11-04
@ilyaalekseev

It's not very clear what you mean by

get

Maybe you want to group products by users - Example

N
Nikita Gushchin, 2015-11-04
@iNikNik

I'll do my part. I will be based on the answer of Alexey Zuev :
1. Make a separate component for the row with the product (GoodRow) and the row with the user's goods (UserGoodsRow). Still different things.
2. Add a key property to all dynamic child components
3. Never capitalize object fields. Fi. Yes, and CamelCase is preferable to SnakeCase
Result -> jsfiddle.net/yuvuyrhv/11
4. How do you feel about ES6? You still need babel!
jsfiddle.net/yuvuyrhv/13
5. Specify propTypes
And finally, I will say that the logic of partitioning products by users is REQUIREDmove to a separate layer (either in business ... or, at worst, in some controller-view like UserGoodsPage)
Final result -> jsfiddle.net/yuvuyrhv/14
PS I also recommend using one of the tools that make FP easier - be it lodash, underscore or ramdajs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question