【发布时间】:2017-08-12 23:58:45
【问题描述】:
我已经使用 react 创建了一个游戏,现在我正在尝试对我的代码+样式进行必要的更改,以便游戏通过 ReactNative 在移动设备上运行。以下代码来自有关FlatList 的官方指南。
1. class MyList extends React.PureComponent {
2. state = {selected: (new Map(): Map<string, boolean>)};
3. _keyExtractor = (item, index) => item.id;
4. _onPressItem = (id: string) => {
// updater functions are preferred for transactional updates
5. this.setState((state) => {
// copy the map rather than modifying state.
6. const selected = new Map(state.selected);
7. selected.set(id, !selected.get(id)); // toggle
8. return {selected}; }); };
虽然我之前使用过 es6 箭头函数和 Maps,但老实说,我很难理解以下两种情况下使用的语法:
a) 第 2 行:(new Map(): Map<string, boolean>) 假设返回什么?
b) 第 4 行:一个箭头函数,它以一个...符号作为参数?为什么?
【问题讨论】:
标签: react-native react-native-flatlist