1

在这家呆了很长时间,并不能完全弄清楚。

我有两个组件,由父组件控制。有一个属性叫做“selected”。因此,当用户单击列表时,它将更新selected传递给的父组件的属性,该属性TagInput使用MentionPluginfrom draft-js。

为了处理这个问题,我实现了一个componentWillReceiveProps如下所示的。

componentWillReceiveProps(nextProps) {
  const { initialTags: newTags } = nextProps;
  const previousTags = this.getTags(this.state.editorState);

  if (previousTags.length !== newTags.length) {
    const added = newTags.filter(tag => !previousTags.includes(tag));
    const removed = previousTags.filter(tag => !newTags.includes(tag));

    this.addMentions(added);
    this.removeMentions(removed);
  }
}

虽然通过创建一个新实体并插入它很容易添加实体,addMentions但对于我来说,我无法弄清楚如何Mention通过文本获取一个然后从编辑器中删除它。

removeMentions(tags) {
  const { editorState } = this.state;
  for (const tag of tags) {
    // find tag in editor
    // select it and remove it
  }
}

这将如何完成?

4

0 回答 0