javascript - react 和 typescript 上的传播运算符错误

标签 javascript reactjs typescript

我有一个功能:

handleMarkerClick(targetMarker) {
    this.setState({
        markers: this.state.markers.map(marker => {
            if (marker === targetMarker) {
                return {
                    ...marker, //  error TS1136: Property assignment expected.

                    showInfo: true, // error TS1005: ',' expected.
                }; // error TS1135: Argument expression expected, error TS1005: ')' expected.
            }
            return marker; //  error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
        }), // Declaration or statement expected.
    });
}

我看到很多错误:

  • 预期进行属性分配。
  • 预期为“,”。
  • 需要参数表达式。
  • 您可能需要适当的加载程序来处理此文件类型。 |导出.__esModule = true; |导出[“默认”] = PopUpInfoWindowExample; |返回标记;

但是当我删除“标记”附近的“...”时,我的功能无法正常工作。

我这样做:

    handleMarkerClick(targetMarker) {
    this.setState({
        markers: this.state.markers.map(marker => {
            if (marker === targetMarker) {
                return {
                    marker, // delete ...
                    showInfo: true,
                };
            }
            return marker;
        }),
    });
}
为什么?我该怎么做才能获得正确的功能?

最佳答案

如果marker是一个对象,那么您可以尝试使用Object.assign(),而不是使用扩展运算符。这是一个例子:

marker = Object.assign({}, marker);

关于javascript - react 和 typescript 上的传播运算符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41325678/

相关文章:

typescript - Angularjs 2.0 不加载 templateUrl

typescript - 为什么我们应该使用 “extends” 关键字而不是类型本身?

reactjs - react 高阶组件在 typescript 中不起作用

javascript - 动态导入或需要 React 组件

javascript - Axios 循环 promise

javascript - 在 THREE.js 中,如何将一个纹理映射到 3D 矩形

regex - xregexp 有不同的结果

javascript - 如何通过点击div数组来过滤记录?

javascript - 如何自定义jquery-jtable创建对话框

php - 处理、解析和流式传输大型文本文件的实用方法是什么?