![](/img/trans.png)
[英]ES6 Spread operator (Rest Parameter) - syntax fails with Safari 9
[英]Spread or Rest operator clarification ES6
我正在关注反应教程,我需要一点澄清。 在App.js组件的render函数中,spread运算符用于通过contests数组映射公开的竞赛对象。 ContestPreview.js组件如何在没有命名属性的情况下将其用作道具,为什么在此实例中使用扩展运算符?
谢谢。
App.js
import React from 'react';
import Header from './Header';
import ContestPreview from './ContestPreview';
import data from '../testData';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
pageHeader: 'Naming Contests',
contests: []
};
}
componentDidMount() {
this.setState({
contests: data.contests
});
}
render() {
return (
<div className='App'>
<Header message={this.state.pageHeader} />
<div>
{this.state.contests.map(contest =>
<ContestPreview key={contest.id} {...contest}
/>
)};
</div>
</div>
);
}
}
export default App;
ContestPreview.js
import React from 'react';
const ContestPreview = (contest) => (
<div className='ContestPreview'>
<div className='category-name'>
{contest.categoryName}
</div>
<div className='contest-name'>
{contest.contestName}
</div>
</div>
);
export default ContestPreview;
在上下文中
<div {...props} />
这里的扩展语法( ...
)特别是JSX,它有自己的JSX定义行为,因为它不是正常的ES6扩展,也不是建议的对象扩展。
JSX扩展语法将获取给定对象的所有属性,并将它们作为props
一部分传递给子组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.