简体   繁体   English

添加/删除React Grid布局示例:TypeError:“ exports”为只读

[英]Add/Remove React Grid Layout Example: TypeError: “exports” is read-only

I am having difficulties getting this code example from the ReactGridLayout library to work. 我很难从ReactGridLayout库中获取此代码示例,以使其正常工作。 I think I have solved some of the issues with pathing for css files, but I am not sure what to make of this error. 我想我已经解决了css文件路径中的一些问题,但是我不确定该怎么办。

Link to functional exmaple: https://strml.github.io/react-grid-layout/examples/6-dynamic-add-remove.html 链接到功能示例: https ://strml.github.io/react-grid-layout/examples/6-dynamic-add-remove.html

import React from "react";
import { WidthProvider, Responsive } from "react-grid-layout";
import _ from "lodash";
const ResponsiveReactGridLayout = WidthProvider(Responsive);

/**
 * This layout demonstrates how to use a grid with a dynamic number of elements.
 */
class AddRemoveLayout extends React.PureComponent {
  static defaultProps = {
    className: "layout",
    cols: { lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 },
    rowHeight: 100
  };

  constructor(props) {
    super(props);

    this.state = {
      items: [0, 1, 2, 3, 4].map(function(i, key, list) {
        return {
          i: i.toString(),
          x: i * 2,
          y: 0,
          w: 2,
          h: 2,
          add: i === (list.length - 1).toString()
        };
      }),
      newCounter: 0
    };

    this.onAddItem = this.onAddItem.bind(this);
    this.onBreakpointChange = this.onBreakpointChange.bind(this);
  }

  createElement(el) {
    const removeStyle = {
      position: "absolute",
      right: "2px",
      top: 0,
      cursor: "pointer"
    };
    const i = el.add ? "+" : el.i;
    return (
      <div key={i} data-grid={el}>
        {el.add ? (
          <span
            className="add text"
            onClick={this.onAddItem}
            title="You can add an item by clicking here, too."
          >
            Add +
          </span>
        ) : (
          <span className="text">{i}</span>
        )}
        <span
          className="remove"
          style={removeStyle}
          onClick={this.onRemoveItem.bind(this, i)}
        >
          x
        </span>
      </div>
    );
  }

  onAddItem() {
    /*eslint no-console: 0*/
    console.log("adding", "n" + this.state.newCounter);
    this.setState({
      // Add a new item. It must have a unique key!
      items: this.state.items.concat({
        i: "n" + this.state.newCounter,
        x: (this.state.items.length * 2) % (this.state.cols || 12),
        y: Infinity, // puts it at the bottom
        w: 2,
        h: 2
      }),
      // Increment the counter to ensure key is always unique.
      newCounter: this.state.newCounter + 1
    });
  }

  // We're using the cols coming back from this to calculate where to add new items.
  onBreakpointChange(breakpoint, cols) {
    this.setState({
      breakpoint: breakpoint,
      cols: cols
    });
  }

  onLayoutChange(layout) {
    this.props.onLayoutChange(layout);
    this.setState({ layout: layout });
  }

  onRemoveItem(i) {
    console.log("removing", i);
    this.setState({ items: _.reject(this.state.items, { i: i }) });
  }

  render() {
    return (
      <div>
        <button onClick={this.onAddItem}>Add Item</button>
        <ResponsiveReactGridLayout
          onLayoutChange={this.onLayoutChange}
          onBreakpointChange={this.onBreakpointChange}
          {...this.props}
        >
          {_.map(this.state.items, el => this.createElement(el))}
        </ResponsiveReactGridLayout>
      </div>
    );
  }
}

module.exports = AddRemoveLayout;

if (require.main === module) {
  require("../test-hook.jsx")(module.exports);
}

As per https://github.com/webpack/webpack/issues/3997 and https://github.com/webpack/webpack/releases/tag/v2.2.0-rc.5 根据https://github.com/webpack/webpack/issues/3997https://github.com/webpack/webpack/releases/tag/v2.2.0-rc.5

module.exports is read-only and undefined module.exports为只读且未定义

We need to use export default AddRemoveLayout 我们需要使用export default AddRemoveLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的 TypeError 导出是只读的 - Uncaught TypeError exports is read-only 使用Webpack和Vue-CLI,间歇性“TypeError:”导出“只读”错误 - Intermittent “TypeError: ”exports“ is read-only” error with Webpack and Vue-CLI TypeError: &quot;exports&quot; is read-only =&gt; 使用调用另一个方法的方法导出模块时 - TypeError: "exports" is read-only => When exporting a module with method calling another method 类型错误:“状态”是只读的 - TypeError: "state" is read-only TypeError:“stack”是只读的 - TypeError: “stack” is read-only 添加文档后,如何在 React Native 中保存 Firestore 响应(可能未处理的 Promise 拒绝(id:1):TypeError:“res”是只读的)? - How to save Firestore response, after I add a doc, in React Native (Possible Unhandled Promise Rejection (id: 1): TypeError: "res" is read-only)? Firefox浏览器。 TypeError:0为只读 - Firefox. TypeError: 0 is read-only dhtmlx网格只读复选框 - dhtmlx grid read-only checkbox React 网格布局 - 带有添加/删除小部件的本地存储 - React grid layout - Local storage with add/remove widgets React prop的本地副本是只读的 - Local copy of React prop is read-only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM