3

我被困在如何拖动组件时,我的控制台告诉我,当它被声明为 const 时,我未能清除渲染函数中的 propType { connectDragSource, isDraggin } = this.props;。我注意到如果我从 KnightSource 对象中删除 is Dragging 属性,它会修复此错误,但我不知道为什么。

import React, { Component, PropTypes } from 'react';
import { ItemTypes } from './Constants';
import { DragSource } from 'react-dnd';

const knightSource = {
  beginDrag(props) {
    return {};
  },

  isDragging(props){
      return(console.log('dragging'))
  }
};

function collect(connect, monitor) {
  return {
    connectDragSource: connect.dragSource(),
    isDragging: monitor.isDragging()
  }
}

class Knight extends Component {
  render() {
    const { connectDragSource, isDragging } = this.props;
    return connectDragSource(
      <div style={{
        opacity: isDragging ? 0.5 : 1,
        fontSize: 100,
        fontWeight: 'bold',
        cursor: 'move',
        color: isDragging ? 'blue' : 'green'
      }}>
        ♘
      </div>
    );
  }
}

Knight.propTypes = {
  connectDragSource: PropTypes.func.isRequired,
  isDragging: PropTypes.bool.isRequired
};

export default DragSource(ItemTypes.STUDENT, knightSource, collect)(Knight);
4

1 回答 1

0

isDragging(props){return(console.log('dragging'))} 有效的javascript吗?我认为您不能将 console.log 作为返回的值。

于 2016-05-28T18:21:44.960 回答