从对象数组渲染React组件

tha*_*guy 82 javascript reactjs

我有一些叫做station的数据,它是一个包含对象的数组.

stations : [
  {call:'station one',frequency:'000'},
  {call:'station two',frequency:'001'}
]
Run Code Online (Sandbox Code Playgroud)

我想为每个数组位置渲染一个ui组件.到目前为止我可以写

 var stationsArr = []
 for (var i = 0; i < this.data.stations.length; i++) {
     stationsArr.push(
         <div className="station">
             {this.data}
         </div>
     )
 }
Run Code Online (Sandbox Code Playgroud)

然后渲染

render(){
 return (
   {stationsArr}
 )
}
Run Code Online (Sandbox Code Playgroud)

问题是我正在打印所有数据.我想要只显示一个键,{this.data.call}但不打印任何东西.

如何遍历此数据并为数组的每个位置返回一个新的UI元素?

Seb*_*ber 135

您可以将站列表映射到ReactElements.

使用React> = 16,可以从同一组件返回多个元素,而无需额外的html元素包装.从16.2开始,有一个新的语法<>来创建片段.如果这不起作用或IDE不支持,则可以<React.Fragment>改为使用.在16.0和16.2之间,您可以使用非常简单的polyfill作为碎片.

请尝试以下方法

// Modern syntax >= React 16.2.0
const Test = ({stations}) => (
  <>
    {stations.map(station => (
      <div className="station" key={station.call}>{station.call}</div>
    ))}
  </>
); 

// Modern syntax < React 16.2.0
// You need to wrap in an extra element like div here

const Test = ({stations}) => (
  <div>
    {stations.map(station => (
      <div className="station" key={station.call}>{station.call}</div>
    ))}
  </div>
); 

// old syntax
var Test = React.createClass({
    render: function() {
        var stationComponents = this.props.stations.map(function(station) {
            return <div className="station" key={station.call}>{station.call}</div>;
        });
        return <div>{stationComponents}</div>;
    }
});

var stations = [
  {call:'station one',frequency:'000'},
  {call:'station two',frequency:'001'}
]; 

ReactDOM.render(
  <div>
    <Test stations={stations} />
  </div>,
  document.getElementById('container')
);
Run Code Online (Sandbox Code Playgroud)

不要忘记key属性!

https://jsfiddle.net/69z2wepo/14377/

  • @thatgibbyguy在这种情况下它并没有带来太大的好处.在更高级的示例中,它允许具有更好的渲染性能,因为React可以轻松地知道现有节点是否已移动到站阵列中的另一个位置,从而避免破坏和重新创建现有的dom节点(并且还保持dom节点已安装) .它位于反应文档中:https://facebook.github.io/react/docs/reconciliation.html#keys (4认同)

Tho*_*dez 40

对于像我这样的新手,我的答案可能会有点混乱.您可以map在组件渲染方法中使用.

render () {
   return (
       <div>
           {stations.map(station => <div> {station} </div>)} 
       </div>
   );
}
Run Code Online (Sandbox Code Playgroud)

  • 这需要一个`key` prop https://reactjs.org/docs/lists-and-keys.html#keys (8认同)
  • 这基本上是接受的答案所说的. (3认同)

Dom*_*nic 6

this.data 可能包含所有数据,因此您需要执行以下操作:

var stations = [];
var stationData = this.data.stations;

for (var i = 0; i < stationData.length; i++) {
    stations.push(
        <div key={stationData[i].call} className="station">
            Call: {stationData[i].call}, Freq: {stationData[i].frequency}
        </div>
    )
}

render() {
  return (
    <div className="stations">{stations}</div>
  )
}
Run Code Online (Sandbox Code Playgroud)

或者map,如果您使用的是ES6,则可以使用和箭头功能:

const stations = this.data.stations.map(station =>
    <div key={station.call} className="station">
      Call: {station.call}, Freq: {station.frequency}
    </div>
);
Run Code Online (Sandbox Code Playgroud)

  • 这在当前的React版本中不起作用,您无法返回数组. (2认同)