React Draft JS 获取当前选择(正在选择上一个文本块)

React Draft JS get current selection (previous text block is being selected)

我正在尝试获取当前选择文本甚至块,我在文档中找到了以下代码

var selectionState = editorState.getSelection();
var anchorKey = selectionState.getAnchorKey();
var currentContent = editorState.getCurrentContent();
var currentContentBlock = currentContent.getBlockForKey(anchorKey);
var start = selectionState.getStartOffset();
var end = selectionState.getEndOffset();
var selectedText = currentContentBlock.getText();

但是selectedText是之前选择的块意思

块1(用户第一次点击)
区块 2(用户第二次点击)
它将显示块 1,然后显示
单击块 1(第三次)将显示块 2

这就像你必须双击才能显示正确的方块

有什么解决办法吗?谢谢

问题是我使用的是以前的状态而不是最新的状态,这是可行的解决方案

const getCurrentBlock = (editorState) => {
    const currentSelection = editorState.getSelection();
    const blockKey = currentSelection.getStartKey();
    return(editorState.getCurrentContent().getBlockForKey(blockKey));
}

const getCurrentText = (editorState) => {
    const currentBlock = getCurrentBlock(editorState);
    const blockText = currentBlock.getText();
    return blockText;
}

const changeEditorState = (state) => {console.log(getCurrentText(state));}