React-dnd: Nicht erfasster Fehler: Es können nicht zwei HTML5-Backends gleichzeitig vorhanden sein.

Erstellt am 20. Apr. 2017  ·  3Kommentare  ·  Quelle: react-dnd/react-dnd

Ich versuche, einen POC mit Rails5, Action Cable, React and Rails und React DnD zu erstellen.

Der Zweck besteht darin, eine App wie Trello zu erstellen, jedoch für einen Rekrutierungsprozess.

Meine Front ist in ReactJS.

Ich habe 3 Komponenten, zuerst den Containeraufruf "Candidates", diese Komponente ruft 2 "CardBoard" -Komponenten auf, die die "Card" -Komponente aufrufen.

Ich nutze die DnD-Bibliothek für Draggable Card und Droppable CardBoard. Wenn ich die Karte auf Pappe lege, verwende ich einen Post-Call und ein Websocket (Action-Kabel von Rails5), um meinen Status zu aktualisieren. Ich verstehe nicht, warum ich nach dem Postanruf diese Nachricht bekomme:

Uncaught Error: Cannot have two HTML5 backends at the same time.
    at HTML5Backend.setup (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:4175), <anonymous>:87:15)
    at DragDropManager.handleRefCountChange (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:3566), <anonymous>:52:22)
    at Object.dispatch (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:4931), <anonymous>:186:19)
    at HandlerRegistry.addSource (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:3594), <anonymous>:104:18)
    at registerSource (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:4294), <anonymous>:9:27)
    at DragDropContainer.receiveType (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:1793), <anonymous>:146:32)
    at DragDropContainer.receiveProps (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:1793), <anonymous>:135:14)
    at new DragDropContainer (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:1793), <anonymous>:102:13)
    at eval (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:4399), <anonymous>:295:18)
    at measureLifeCyclePerf (eval at <anonymous> (webpack-bundle.self-7b1a342….js?body=1:4399), <anonymous>:75:12)

Kandidat.jsx =

import React, { PropTypes } from 'react';
import { DragDropContextProvider } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import CardBoard from './CardBoard.jsx';

export default class Candidates extends React.Component {

  constructor(props, _railsContext) {
    super(props);

    this.state = {
      candidates: this.props.candidates
    }

    this.filterByStatus = this.filterByStatus.bind(this)
  }

  componentDidMount() {
    this.setupSubscription();
  }

  setupSubscription() {
    App.candidate = App.cable.subscriptions.create("CandidateChannel", {
      connected: () => {
        console.log("User connected !")
      },

      received: (data) => {
        this.setState({ candidates: data.candidates })
      },
    });
   }

  render() {
    return (
      <DragDropContextProvider backend={HTML5Backend}>
        <div className="recruitment">
          {
            ["À Rencontrer", "Entretien"].map((status, index) => {
              return (
                <CardBoard candidates={(this.filterByStatus({status}))} status={status} key={index} />
              );
            })
          }
        </div>
      </DragDropContextProvider>
    );
  } 
}

CardBoard.jsx =

import React, { PropTypes } from 'react';
import Card from './Card.jsx';
import { DropTarget } from 'react-dnd';
import ItemTypes from './ItemTypes';

const cardTarget = {
  drop(props: Props) {
    var status = ''

    if(props.status == "À Rencontrer") {
      status = 'to_book'
    } else {
      status = 'interview'
    }

    return { status: status };
  },
};

@DropTarget(ItemTypes.CARD, cardTarget, (connect, monitor) => ({
  connectDropTarget: connect.dropTarget(),
  isOver: monitor.isOver(),
  canDrop: monitor.canDrop(),
}))

export default class CardBoard extends React.Component<Props> {

  constructor(props, _railsContext) {
    super(props);
  }

  render() {
    const { canDrop, isOver, connectDropTarget } = this.props;
    const isActive = canDrop && isOver;


    return connectDropTarget(
      <div className={`${this.props.status} ui cards`}>
        <h2>{`${this.props.status} (${this.props.candidates.length})`}</h2>
        {
          (this.props.candidates).map((candidate, index) => {
            return <Card candidate={candidate} key={index} />
          })
        }
        { isActive?
          'Release to drop' : 'drag a card here'
        }
      </div>
    );
  }
}

Karte.jsx=

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


const cardSource = {
  beginDrag(props) {
    return {
      candidate_id: props.candidate.id,
    };
  },

  endDrag(props, monitor) {
    const item = monitor.getItem();
    const dropResult = monitor.getDropResult();

    if (dropResult) {
      console.log(`You dropped ${item.candidate_id} vers ${dropResult.status} !`);
      $.post(`/update_status/${item.candidate_id}/${dropResult.status}`);
    }
  },
};

@DragSource(ItemTypes.CARD, cardSource, (connect, monitor) => ({
  connectDragSource: connect.dragSource(),
  isDragging: monitor.isDragging(),
}))

export default class Card extends React.Component {

  constructor(props, _railsContext) {
    super(props);
  }

  render() {
    const { isDragging, connectDragSource } = this.props;
    const { name } = this.props;
    const opacity = isDragging ? 0 : 1;

    var candidate = this.props.candidate;

    return (
      connectDragSource(
        <div className="card" key={candidate.id} style={{opacity}}>
          <div className="content">
            <div className="header">{`${candidate.first_name} ${candidate.last_name}`}</div>
            <div className="description">
              {candidate.job_title}
            </div>
            <span className="right floated">
              <i className="heart outline like icon"></i>
              {candidate.average_rate}
            </span>
          </div>
        </div>
      )
    );
  }
}

zum besseren verständnis hier ein gif von meinem feature und seinem bug :
bugcard

wontfix

Hilfreichster Kommentar

@antoineBernard Ich habe kürzlich einen ganzen Tag damit verbracht, denselben Fehler zu überwinden. Es könnte ein ähnliches Problem wie bei dir sein, bin mir aber nicht sicher. Versuchen Sie eines von zwei Dingen:

  1. Versuchen Sie, Ihr <DragDropContextProvider> in die übergeordnete Komponente von Candidates.jsx zu verschieben.
  2. Verwenden Sie das Singleton-Muster, um sicherzustellen, dass in Ihrer App nur eine einzige Instanz von DragDropContext initialisiert wird. Ich habe den Vorschlag von @gcorne mit Erfolg verwendet.

Hoffe es funktioniert auch bei dir!

Alle 3 Kommentare

@antoineBernard Ich habe kürzlich einen ganzen Tag damit verbracht, denselben Fehler zu überwinden. Es könnte ein ähnliches Problem wie bei dir sein, bin mir aber nicht sicher. Versuchen Sie eines von zwei Dingen:

  1. Versuchen Sie, Ihr <DragDropContextProvider> in die übergeordnete Komponente von Candidates.jsx zu verschieben.
  2. Verwenden Sie das Singleton-Muster, um sicherzustellen, dass in Ihrer App nur eine einzige Instanz von DragDropContext initialisiert wird. Ich habe den Vorschlag von @gcorne mit Erfolg verwendet.

Hoffe es funktioniert auch bei dir!

Das hat bei mir funktioniert: https://github.com/react-dnd/react-dnd/issues/186#issuecomment -282789420

Dieses Problem wurde automatisch als veraltet markiert, da es in letzter Zeit keine Aktivität gab. Es wird geschlossen, wenn keine weiteren Aktivitäten stattfinden. Vielen Dank für Ihre Beiträge.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen