【问题标题】:React Dragula giving "Failed to compile" and "unexpected" errorReact Dragula 给出“编译失败”和“意外”错误
【发布时间】:2017-06-27 15:50:03
【问题描述】:

我一直在尝试实现 React Dragula(链接:https://github.com/bevacqua/react-dragula)并不断出现错误,因为我确定我的语法不正确。有人可以让我知道我在这里做错了什么吗?

我要做的就是让下面的<div> 成为一个可排序的拖放列表(这证明比我希望的要困难得多)。 React DND 是一种选择,但是我遇到了很多问题,这似乎更简单一些。

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {Icon} from 'react-fa';
import { Row, Col } from 'react-flexbox-grid';
import styled from 'styled-components';
import Dragula from 'react-dragula';

const style = {
  cursor: 'move',
};


const CountCell = styled.div`
  border: 1px solid #5C57B1;
  background: #6E68C5
  width: 320px;
  display: flex;
  justify-content: center;
  height: 50px;
  align-items: center;
`

const Score = styled.span`
  color: #74D8FF;
  font-size: 26px;
  font-weight: bold;
  padding: 0 0.5em;
  display: inline-block;
  width: 30px;
  text-align: center;
`
const ScoreName = styled.span`
  color: white;
  font-size: 20px;
  padding: 0 0.5em;
  display: inline-block;
  width: 160px;
  text-align: center;
`

const CountButton = styled.button`
  display: flex;
  justify-content: center;
  align-items: center;
  align-content: center;
  background: #6E68C5;
  height: 30px;
  border: 0;
  border-radius: 50px;
  border: 3px solid white;
  width: 30px;
  transition: all 250ms;
  &:focus {outline:0;}
  &:hover {background: white;}
`

class Counter extends Component {

  incrementCount(e) {
    // I need to update the current state's count, and add 1 to it.
    this.setState({
      count: (this.state.count + 1),
    })
  }

  decrementCount(e) {
    this.setState({
      count: (this.state.count - 1),
    })
  }


  render() {
    const { count } = this.props
    const { decrementCount } = this.props
    const { incrementCount } = this.props
    const { nameof } = this.props
    const { text, isDragging, connectDragSource, connectDropTarget } = this.props;
    const opacity = isDragging ? 0 : 1;
    return (
      <div className='container' style={{ ...style, opacity }} ref={this.dragulaDecorator}>
          <CountCell>
            <Row style={{alignItems: 'center'}}>
              <Col>
                <CountButton
                  onClick={incrementCount}>
                  <Icon
                    name="icon" className="fa fa-plus score-icon"
                  />
                </CountButton>
              </Col>
              <Col >
                <ScoreName>{nameof}</ScoreName>
              </Col>
              <Col >
                <Score>{count}</Score>
              </Col>
              <Col>
                <CountButton
                  onClick={decrementCount}>
                  <Icon
                  name="icon" className="fa fa-minus score-icon"
                  />
                </CountButton>
              </Col>
            </Row>
          </CountCell>
        </div>
      )
    }
  }

Counter.propTypes = {
  // We are going to _require_ a prop called "count". It _has_ to be a Number.
  count: PropTypes.number.isRequired,

  // We are going to _require_ a prop called "incrementCount". It _has_ to be a Function.
  incrementCount: PropTypes.func.isRequired,

  // We are going to _require_ a prop called "decrementCount". It _has_ to be a Function.
  decrementCount: PropTypes.func.isRequired,

  nameof: PropTypes.string.isRequired,
},
componentDidMount: function () {
  var container = React.findDOMNode(this);
  dragula([container]);
}
});

export default Counter

我得到的错误是:

./src/components/pages/projectpages/dnd2/Counter.js
Syntax error: Unexpected token, expected ; (127:17)

  125 |   nameof: PropTypes.string.isRequired,
  126 | },
> 127 | componentDidMount: function () {
      |                  ^
  128 |   var container = React.findDOMNode(this);
  129 |   dragula([container]);
  130 | }

【问题讨论】:

  • 你有什么错误吗?
  • 是的,我也会更新我的帖子,错误是这个./src/components/pages/projectpages/dnd2/Counter.js Syntax error: Unexpected token, expected ; (127:17) 125 | nameof: PropTypes.string.isRequired, 126 | }, &gt; 127 | componentDidMount: function () { | ^ 128 | var container = React.findDOMNode(this); 129 | dragula([container]); 130 | }
  • 更新了我的问题,使其可读
  • 检查第 126 行。您正在关闭Counter.PropTypes
  • 我现在得到一个Unexpected tokencomponentDidMount: function () {

标签: javascript reactjs ecmascript-6 draggable


【解决方案1】:

你的组件应该是这样的吗?

import Dragula from 'react-dragula';

class Counter extends Component {

    incrementCount(e) {
        // I need to update the current state's count, and add 1 to it.
        this.setState({
            count: (this.state.count + 1),
        })
    }

    decrementCount(e) {
        this.setState({
            count: (this.state.count - 1),
        })
    }
    render() {
        const { count } = this.props
        const { decrementCount } = this.props
        const { incrementCount } = this.props
        const { nameof } = this.props
        const { text, isDragging, connectDragSource, connectDropTarget } = this.props;
        const opacity = isDragging ? 0 : 1;
        return (
            <div className='container' style={{ ...style, opacity }} ref={this.dragulaDecorator}>
                <CountCell>
                    <Row style={{ alignItems: 'center' }}>
                        <Col>
                            <CountButton
                                onClick={incrementCount}>
                                <Icon
                                    name="icon" className="fa fa-plus score-icon"
                                />
                            </CountButton>
                        </Col>
                        <Col >
                            <ScoreName>{nameof}</ScoreName>
                        </Col>
                        <Col >
                            <Score>{count}</Score>
                        </Col>
                        <Col>
                            <CountButton
                                onClick={decrementCount}>
                                <Icon
                                    name="icon" className="fa fa-minus score-icon"
                                />
                            </CountButton>
                        </Col>
                    </Row>
                </CountCell>
            </div>
        )
    }
    dragulaDecorator = (componentBackingInstance) => {
        if (componentBackingInstance) {
            let options = {};
            Dragula([componentBackingInstance], options);
        }
    };
}

Counter.propTypes = {
  // We are going to _require_ a prop called "count". It _has_ to be a Number.
  count: PropTypes.number.isRequired,

  // We are going to _require_ a prop called "incrementCount". It _has_ to be a Function.
  incrementCount: PropTypes.func.isRequired,

  // We are going to _require_ a prop called "decrementCount". It _has_ to be a Function.
  decrementCount: PropTypes.func.isRequired,

  nameof: PropTypes.string.isRequired,
}

export default Counter;

https://codesandbox.io/s/N9k0K0Lpp

【讨论】:

  • 在第 75 行:dragula([container]);'dragula' is not defined no-undef
  • 只是确保它不应该是Dragula 对吧?
  • @sthig 我已经编辑了代码,因为它应该在您的变体中,并检查链接,那里有 Dragula 的基本用法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
相关资源
最近更新 更多